Monitor the content changes of edittext and make corresponding processing.
Mainactivity.class
PackageCom.example.edittextdemo;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.text.Editable;ImportAndroid.text.TextWatcher;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.View.OnFocusChangeListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {EditText edit_phone; Button Btn_next; String Okdata; TextView Monitor_edit; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); EditText edit_id=(EditText) Findviewbyid (r.id.edit_id); Btn_next=(Button) Findviewbyid (R.id.btn_next); Monitor_edit=(TextView) Findviewbyid (R.id.monitor_edit); Btn_next.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {if(!Isid (Okdata)) {Monitor_edit.settext ("Please enter the correct ID number!" "); } } }); //monitoring of EditText FocusEdit_id.setonfocuschangelistener (NewOnfocuschangelistener () {@Override Public voidOnfocuschange (View V,BooleanHasfocus) { //when Hasfocus = True is the Get focus if(!Hasfocus) { if(!Isid (Okdata)) {Monitor_edit.settext ("Please enter the correct ID number!" "); } } } }); //real-time monitoring of edittext contentEdit_id.addtextchangedlistener (NewTextwatcher () {//a second execution@Override Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) {System.out.println ("OnTextChanged:" + "start:" + Start + "before:" + before + "Count:" +count); } //The first one to execute@Override Public voidBeforetextchanged (Charsequence S,intStartintCountintAfter ) {System.out.println ("Beforetextchanged:" + "start:" + Start + "Count:" + Count + "after:" +After ); } //a third implementation@Override Public voidAftertextchanged (Editable s) {//Real-time content in EditTextSystem.out.println ("aftertextchanged:" +s); Okdata=s.tostring (); if(Isid (s.tostring ())) {Monitor_edit.settext ("Right!"); }Else{Monitor_edit.settext ("Please enter the correct ID number!" "); } } }); } /*** is the ID card * *@paramIdstr *@return */ Public Static Booleanisid (String idstr) {string regex= "^[0-9]{17}[0-9|xx]{1}$"; if(Idstr = =NULL|| "". Equals (Idstr.trim ())) { return false; } Else if(Idstr.matches (regex)) {return true; } return false; }}
Activity_main.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "> <EditText Android:id= "@+id/edit_id"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@null"Android:hint= "Enter ID number"/> <TextView Android:id= "@+id/monitor_edit"Android:layout_width= "Match_parent"Android:layout_height= "20DP"Android:textcolor= "#FF0000"/> <EditText android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "19DP"Android:background= "@null"Android:hint= "Enter Name"/> <Button Android:id= "@+id/btn_next"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Next (imitation)"/></linearlayout>
Android EditText Content Monitoring