Textwatcher is an interface for listening to text changes, which makes it easy to listen to and modify text in the text control and editable text controls that can be displayed.
Three methods are defined in the Textwatcher interface:
public void beforetextchanged (Charsequence s, int. start, int count, int after) {}
This method is called before the text changes, and four parameters are passed in:
- Charsequence S: Content before the text changes
- int start: Start position when text starts to change, calculated from 0
- int count: The number of text words to be changed, which is the number of selected text that will be replaced
- int after: The number of text words added after the change, that is, the number of text after the selected text
The method call is called when the text is not changed, but is about to be changed, and the four parameters are composed in a sentence:
In the current text s, count characters from start position (impending) are replaced by after characters
public void ontextchanged (charsequence s, int start, int before, int count) {}
This method is called when the text is changed, and the same four parameters are passed in:
- Charsequence s: Content after text changes
- int start: Start position when text starts to change, calculated from 0
- int before: The number of text words to be changed, that is, the number of selected text that has been replaced
- int count: The number of text words added after the change, that is, the number of words to replace the selected text
The method call is called when the text is changed, the result of the change can already be displayed, and the four parameters consist of a sentence:
In the current text s, the before character (already) after the start position has been replaced by count characters
public void aftertextchanged (Editable s) {}
The method is called after the text change is complete, and a parameter is passed in:
- Editable S: The final text after the change
This method is called after executing the beforetextchanged, ontextchanged two methods, where the text s is the text that is ultimately displayed to the user. We can then proceed to the next step of the text, such as displaying the text s on the UI interface
Practice using Textwatcher Monitor EditText remaining can enter the number of text words
There is a need: Users can enter only 50 characters in EditText, the user input at the same time in the UI interface to tell users can also enter how many characters
Layout file Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?><linearlayoutxmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Match_parent"android:layout_height="Match_parent" Android:background="@android: Color/white"android:orientation="vertical" > <!--text input box -- <EditText Android:id="@+id/edt_text_content" Android:layout_width="Match_parent" Android:layout_height="Wrap_content" Android:layout_margin="10DP" Android:background="@drawable/bg_edit_radius" android:gravity="Top" Android:maxlength=" the" Android:minlines="5" android:padding="10DP" Android:textcolor="@android: Color/white" android:textsize="20SP"/> <!--input words -- <linearlayoutandroid:layout_width="Match_parent"android:layout_height ="Wrap_content"android:layout_margin="10DP"android:orientation ="Horizontal"> <textview android:layout _width = "wrap_content" android:layout_height = "wrap_content" android:text = "input words:" android:textsize = "16sp" /> <TextViewandroid:id="@+id/tv_text_now_sum"android:layout_width= "Wrap_content"android:layout_height="Wrap_content"android:text="0" Android:textcolor="#ff0000"android:textsize="16sp"/> </linearlayout> <!--remaining can be input - <linearlayout android: Layout_width = "match_parent" android:layout_ Height = "wrap_content" android:layout_margin = "10DP" android:orientation = "horizontal" ; <textview android:layout _width = "wrap_content" android:layout_height = "wrap_content" android:text = "remaining can be entered:" android:textsize = "16sp" /> <TextViewandroid:id= "@+id/tv_text_remain"android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:text=" Android:textcolor="#ff0000"android:textsize="16sp"/> </linearlayout></linearlayout>
In the above layout, the EditText control uses the maxlength= "50" limit to enter a character of 50
The controls and variables you need to use
/** 输入框 */ private EditText mTextContentEdt; /** 剩余字数 */ private TextView mTextRemainTv; /** 已输入字数 */ private TextView mTextNowSumTv; /** 总可输入字数 */ privateint50;
Input box style picture Bg_edit_radius.xml
<?xml version= "1.0" encoding= "Utf-8"?><shape xmlns:android="Http://schemas.android.com/apk/res/android"> <strokeandroid:width="1DP"android:color="@android: Color/black" /> <corners Android:radius="10DP" /> <solid android:color="@android: Color/darker_gray" /></shape>
Initializing the interface
/** * 初始化界面 */privatevoidinitUI() { setContentView(R.layout.activity_main); mTextContentEdt = (EditText) findViewById(R.id.edt_text_content); mTextRemainTv = (TextView) findViewById(R.id.tv_text_remain); mTextNowSumTv = (TextView) findViewById(R.id.tv_text_now_sum);}
Add monitoring to EditText, Mytextwatcher class for our custom listener class
/** * 初始化监听 */privatevoidinitListener() { mTextContentEdt.addTextChangedListener(new MyTextWatcher()); }
Creating a custom Textwatcher listener class to implement the Textwatcher interface
/** * Create your own Textwatcher listening class * *Private class mytextwatcher implements textwatcher { @Override Public void beforetextchanged(Charsequence S,intStartintCountintAfter) {}@Override Public void ontextchanged(Charsequence S,intStartintBefore,intCount) {//Number of words already enteredMtextnowsumtv.settext (String.valueof (S.length ()));//Number of words left to enter intRemainsum = Textremainall-s.length (); Mtextremaintv.settext (string.valueof (remainsum)); }@Override Public void aftertextchanged(Editable s) { }}
After running the
Android Text Listener interface Textwatcher detailed