Android in the edit box EditText is also more commonly used, such as in the search box, did not enter a word, the following search list is displayed with the Input keyword options, this input monitoring how to achieve it?
We can build an example, as follows:
We can listen to where the cursor is, select a few characters and process, enter a few characters
Create a new layout file first
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="fill_parent"Android:layout_height="fill_parent"Android:background="@drawable/af"> <!--scroll up and down <ScrollView android:layout_width="fill_parent"Android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent"Android:layout_height="fill_parent"android:orientation="Vertical"> <!--edit box--<EditText Android:id="@+id/id_edittext_1"Android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:background="@drawable/alert_light"android:textsize="10SP"Android:textcolor="#ffff"/> <TextView Android:id="@+id/id_textview"Android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:textcolor="#ffff"/> <TextView Android:id="@+id/id_textview_1"Android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:background="@drawable/hah"Android:textcolor="#f000"/> <TextView Android:id="@+id/id_textview_2"Android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:background="@drawable/hah"Android:textcolor="#f000"/> </LinearLayout> </ScrollView></LinearLayout>
Then enter the listener event in the code for the edit box binding:
Public classEdittexttestactivity extends Activity {/** edit Box*/ PrivateEditText edit1_; /** Text*/ PrivateTextView Text_; PrivateTextView text1_; PrivateTextView text2_; /** Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); /*set the layout of the current page*/setmylayout (); } /** * Set the layout of the current page*/ Private voidsetmylayout () {/*Get text*/Text_=(TextView) Findviewbyid (R.id.id_textview); Text1_=(TextView) Findviewbyid (r.id.id_textview_1); Text2_=(TextView) Findviewbyid (r.id.id_textview_2); /*Get edit box*/edit1_=(EditText) Findviewbyid (r.id.id_edittext_1); /*Listen for text change events in the edit box*/Edit1_.addtextchangedlistener (NewTextwatcher () {@Override Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) { /*+ + The text changes every time you run this method + +*/ if(NULL!=text_) {Text_.settext ("you are entering ... \ n the current cursor is in the"+Start+"location \ n You chose to process the"+ Before +"characters \ n the words you entered this time have"+ Count +"of characters"); }} @Override Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) { /*+ + The Count branch here is the same as the Before in OnTextChanged () * After the branch is the same as the Count of the ontextchanged () + +*/ if(NULL!=text1_) {Text1_.settext ("you are entering ... \ n the current cursor is in the"+Start+"location \ n You chose to process the"+ Count +"characters \ n the words you entered this time have"+ after +"of characters"); }} @Override Public voidaftertextchanged (Editable s) {/*+ + Here shows the input string + +*/ if(NULL!=text2_) {Text2_.settext (s); } } }); }}
Source code: http://download.csdn.net/detail/zoeice/4399601
Text-Change events in Android monitor EditView