1. gradle
file : build.gradle (Application Level Gradle)
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { applicationId "com.tutorialsbuzz.androidedittext" minSdkVersion 22 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.core:core-ktx:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
2. XML Layout
file : activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" android:gravity="center" android:padding="10dp" android:id="@+id/rootLayout"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint_text" android:id="@+id/editText" android:maxLines="1" android:imeOptions="actionDone" android:inputType="text" android:padding="10dp" /> </LinearLayout>
- android:inputType : value of this attribute aspecifies the type of data that edittext accepts
- none
- text
- textCapCharacters
- textCapWords
- textCapSentences
- textAutoCorrect
- textAutoComplete
- textMultiLine
- textImeMultiLine
- textNoSuggestions
- textUri
- textEmailAddress
- textEmailSubject
- textShortMessage
- textLongMessage
- textPersonName
- textPostalAddress
- textPassword
- textVisiblePassword
- textWebEditText
- textFilter
- textPhonetic
- textWebEmailAddress
- textWebPassword
- number
- numberSigned
- numberDecimal
- numberPassword
- phone
- datetime
- date
- time
- android:maxLines : Set Number of lines user can input
Note : You just need to make sure you have the attribute "android:inputType" set. It doesn't work without inputType.
- android:hint Show hint text
EditText on Editor Change Listener
android:imeOptions this attribute takes keyboard action as value
for example if you want to recognize when action DONE button has clicked ,
- android:imeOptions attribute equal to “actionDone” ,
- Set Editor Change Listener on EditText , override onEditorAction method To Recognize Action DONE.
editText.setOnEditorActionListener { v, actionId, event -> if (actionId == EditorInfo.IME_ACTION_DONE) { Toast.makeText(this@MainActivity, "You Have Entered: " + editText.text.toString(), Toast.LENGTH_SHORT) .show() false } else { false } }
EditText on TextChange Listener
- TextChangeListener monitor whenever there is change of text in EditText
- TextChangeListener interface has 3 method afterTextChanged , beforeTextChanged , onTextChanged
editText.addTextChangedListener(object : TextWatcher { override fun afterTextChanged(s: Editable?) { Log.d(TAG, "afterTextChanged: " + s.toString()); } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { Log.d(TAG, "beforeTextChanged: " + s.toString()); } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { Log.d(TAG, "onTextChanged: " + s.toString()); } })
3. Activity
file : MainActivity.kt
package com.tutorialsbuzz.androidedittext import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.util.Log import android.view.inputmethod.EditorInfo import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { val TAG: String = "MainActivity"; override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //EditText on Editor Change Listener editText.setOnEditorActionListener { v, actionId, event -> if (actionId == EditorInfo.IME_ACTION_DONE) { Toast.makeText(this@MainActivity, "You Have Entered: " + editText.text.toString(), Toast.LENGTH_SHORT) .show() false } else { false } } //EditText on TextChange Listener editText.addTextChangedListener(object : TextWatcher { override fun afterTextChanged(s: Editable?) { Log.d(TAG, "afterTextChanged: " + s.toString()); } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { Log.d(TAG, "beforeTextChanged: " + s.toString()); } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { Log.d(TAG, "onTextChanged: " + s.toString()); } }) } }
No comments:
Post a Comment