EditText is monitored using EditText's addtextchangedlistener (Textwatcher Watcher) method, Textwatcher is an interface class, So the abstract method in Textwatcher must be implemented:
When the content inside the edittext changes, the Textchangedlistener event is triggered and the abstract method inside the Textwatcher is invoked.
Layout:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" fill_parent " android:layout_height=" Fill_parent "><edittext android:id=" @+id/main_et " android:layout_width=" Fill_parent " android: layout_height= "Wrap_content"/> <textview android:id= "@+id/main_tv" android:layout_width= "Fill_ Parent " android:layout_height=" wrap_content "/> </LinearLayout>
First, to achieve the listening text box word count, the hint can also enter how many characters
Mainactivity:
Package Chay.mian;import Android.app.activity;import Android.os.bundle;import android.text.editable;import Android.text.textwatcher;import Android.view.view;import Android.widget.edittext;import Android.widget.TextView; public class Mainactivity extends Activity {private EditText edittext;private TextView tip;private final int charmaxnum = 10; The number of words allowed to enter @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); init ();} private void Init () {EditText = (editText) Findviewbyid (R.id.main_et); Edittext.addtextchangedlistener (new Editchangedlistener ()); tip = (TextView) Findviewbyid (R.ID.MAIN_TV); Tip.settext ("0/" +charmaxnum);} Class Editchangedlistener implements Textwatcher {private charsequence temp;//pre-listening text private int editstart;//cursor start position Priv ate int editend; Cursor End position//state before input text @overridepublic void beforetextchanged (charsequence s, int start, int count,int after) {temp = s;} Enter the state in the text, Count is the one-time input character number @overridepublic void OnTextChanged (charsequence s, int start, int before, int count) {//if (Charmaxnum-s.length () <= 5) {//tip.sette XT ("can also input" + (Charmaxnum-s.length ()) + "character"),//}tip.settext ((S.length ()) + "/" + Charmaxnum);} The state after entering the text @overridepublic void aftertextchanged (Editable s) {/** Gets the cursor start and end position, exceeds the maximum number of records just beyond the number index to control */editstart = Edittext.getselectionstart (); editend = Edittext.getselectionend (); if (Temp.length () > Charmaxnum) {// Toast.maketext (Getapplicationcontext (), "input up to 10 characters", Toast.length_short). Show (); S.delete (editStart-1, editend); Edittext.settext (s); Edittext.setselection (S.length ());}};}
Second, the data type test, when the input is not an integer number, will immediately pop-up input box, prompt to correct
Mainactivity:
Package Chay.mian;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.text.editable;import Android.text.textwatcher;import Android.util.log;import Android.widget.edittext;import Android.widget.TextView; public class Mainactivity extends Activity {private EditText edittext;private TextView tip; String str; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_mian); init ();} private void Init () {EditText = (editText) Findviewbyid (R.id.mian_et); Edittext.addtextchangedlistener (new Editchangedlistener ()); tip = (TextView) Findviewbyid (R.ID.MIAN_TV); Tip.settext ("Please enter integer number");} Class Editchangedlistener implements Textwatcher {//Enter text before the state @overridepublic void beforetextchanged (Charsequence s, int start, int count, int after) {LOG.D ("TAG", "beforetextchanged--------------->");} Enter the state in the text, Count is the one-time input character number @overridepublic void OnTextChanged (CharSequence s, int start, int before, int count) {log.d ("TAG", "ontextchanged--------------->");} Enter the state after the text @overridepublic void aftertextchanged (Editable s) {log.d ("TAG", "aftertextchanged--------------->"); str = Edittext.gettext (). ToString (); try {//if () (Edittext.gettext (). ToString ()) = null) integer.parseint (str);} catch ( Exception e) {showDialog ();}}; private void ShowDialog () {Alertdialog dialog; Alertdialog.builder Builder = new Alertdialog.builder (this); Builder.settitle ("message"). SetIcon (Android. R.drawable.stat_notify_error); Builder.setmessage ("The integer number you output is wrong, please correct"); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stub}});d Ialog = Builder.create ();d ialog.show ();}}
In Logcat view the order in which these methods are called:
Beforetextchanged-->ontextchanged-->ontextchanged
Welcome to AC http://blog.csdn.net/ycwol/article/details/46594275
Android Monitor EditText changes