Monitor EditText text changes need to add a Addtextchangelistener listener to the EditText control
Edittext.addtextchangelistener (Textwatcher); The textwatcher here is a Textwatcher object,
Textwatcher is an interface that has three abstract methods, as follows:
/**
* text changed before calling
* @param the original content string of the input box
* @param start string starts to change the index position
* @param count is about to be replaced by the number of characters
* @param after the number of new characters that are replaced by this count character
Span style= "FONT-SIZE:18PX;" > */ public void beforetextchanged (charsequence s, int start,int count, int.) ;
/**
* Called when text changes
* @param s changes after the input box content
* @param the index position at which start string starts to change
* @param before the number of characters to be replaced
* @param count replaces the number of new characters for this before character
*/ Public void ontextchanged (charsequence s, int start, int before, int count);
/**
* Call after text changes
* @param s content in the final input box
*/ Public void aftertextchanged (Editable s);
Code:
Mainactivity.java
1 PackageCom.example.admin.edittext;2 3 ImportAndroid.os.Bundle;4 Importandroid.support.v7.app.AppCompatActivity;5 Importandroid.text.Editable;6 ImportAndroid.text.TextWatcher;7 ImportAndroid.util.Log;8 9 ImportAndroid.view.View;Ten ImportAndroid.widget.Button; One ImportAndroid.widget.EditText; A ImportAndroid.widget.TextView; - - Importcom.lidroid.xutils.ViewUtils; the Importcom.lidroid.xutils.util.LogUtils; - ImportCom.lidroid.xutils.view.annotation.ViewInject; - - Public classMainactivityextendsappcompatactivity{ + @ViewInject (R.id.text) - PrivateTextView text;//this uses the annotation function of the xutils framework to initialize the control + A @ViewInject (R.id.edittext) at PrivateEditText input; - - @ViewInject (R.id.button) - Privatebutton button; - @Override - protected voidonCreate (Bundle savedinstancestate) { in Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); toViewutils.inject ( This); + initevent (); - } the * Private voidinitevent () { $ Input.addtextchangedlistener (change);Panax NotoginsengButton.setonclicklistener (NewView.onclicklistener () { - @Override the Public voidOnClick (View v) { +Input.settext (""); A } the }); + } - $Textwatcher change =NewTextwatcher () { $ @Override - Public voidBeforetextchanged (Charsequence S,intStartintCountintAfter ) { -Logutils.i ("beforetextchanged:" +s+ "," +start+ "," +count+ "," +After ); the } - Wuyi @Override the Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) { -Text.settext ("can also input" + (50-s.tostring (). Length ()) + "characters"); WuLogutils.i ("Ontextchange:" +s+ "," +start+ "," +before+ "," +count); - } About $ - Public voidaftertextchanged (Editable s) { -LOG.I ("Editable s:", s.tostring ()); - } A }; + the -}
Activity_main.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 >6 7 <TextView8 Android:id= "@+id/text"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One Android:layout_margintop= "20DP" A Android:text= "50 more words can be entered" /> - - <EditText the Android:id= "@+id/edittext" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:layout_below= "@id/text" + Android:maxlength= " the" - /> + <Button A Android:id= "@+id/button" at Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:text= "Reset Text" - Android:textcolor= "#ffffff" - Android:background= "#ff0000" - Android:layout_margintop= "10DP" in Android:layout_below= "@id/edittext" - /> to </Relativelayout>
:
You don't know how much to stand back when you don't spur yourself to get up.
Android EditText text Change monitoring