Android EditText listening and length monitoring event, androidedittext
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Define the basic LinearLayout layout --> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <! -- Define the EditText text input box --> <EditText android: id = "@ + id/Et" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: hint = "Enter the User name:"/> <! -- Define the Button screen area --> <Button android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "myclick" android: text = "Click me, check username validity! "/> <! -- Define TextView text tag --> <TextView android: id = "@ + id/TV" android: layout_width = "fill_parent" android: layout_height = "fill_parent"/> </LinearLayout>
Package com. example. yanlei. yl2; import android. app. alertDialog; import android. content. dialogInterface; import android. content. dialogInterface. onKeyListener; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. app. activity; import android. OS. bundle; import android. text. editable; import android. text. textWatcher; import android. util. log; import android. widget. textView; impor T android. widget. editText; import android. view. view; import android. widget. toast; public class MainActivity extends AppCompatActivity {// defines the TextView object private TextView TV; // defines the EditText object private EditText Et; @ Override public void onCreate (Bundle savedInstanceState) {// callback super when this Activity is created. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findView (); Et. addTextChangedListener (n Ew EditChangedListener ();} private void findView () {// get the control object TV = (TextView) findViewById (R. id. TV); Et = (EditText) findViewById (R. id. et);} // click to call the public void myclick (View v) function bound to the xml file {// obtain the user name entered by the user and obtain the length int len = Et. getText (). toString (). length (); // specify the length of the user name. If (len> 5 & len <9) {TV. setText ("the user name is valid and the length is:" + len);} else {TV. setText ("invalid username length, Length:" + len) ;}} class EditChangedListener implements TextWatcher {private CharSequence temp = ""; // text private int editStart before listening; // start position of the cursor private int editEnd; // end position of the cursor private final int charMaxNum = 10; boolean DEBUG = false; String TAG = "test :"; @ Override public void beforeTextChanged (CharSequence s, int start, I Nt count, int after) {if (DEBUG) Log. I (TAG, "status before text input"); temp = s ;}@ Override public void onTextChanged (CharSequence s, int start, int before, int count) {if (DEBUG) Log. I (TAG, "status in input text, count is the number of characters entered at one time"); TV. setText ("can also input" + (charMaxNum-s. length () + "character") ;}@ Override public void afterTextChanged (Editable s) {if (DEBUG) Log. I (TAG, "status after text input"); try {if (temp. length ()> charMaxNum) {Toast. MakeText (getApplicationContext (), "you have entered more than 10 words! ", Toast. LENGTH_LONG). show () ;}} catch (Exception ex ){}}}}