This function is due to the development of the project, due to the background interface parameters of the length of the value of the requirements, not more than how many characters, so the text entered in the edit box is limited.
Let's take a look at the demo implementation process:
- First, place a EditText control in the XML control, and then initialize the control and add text listeners to the control. XML's own simple design, the code is relatively simple, directly on the code:
PackageCom.example.edittext;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.EditText; Public class mainactivity extends Activity { Private Static Final intLIMIT =Ten;//MAX Word limit PrivateEditText Et_word_limit;//EditText controls @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Get EditText controlEt_word_limit = (EditText) Findviewbyid (R.ID.EDITTEXT1); Setlisteners ();//edit box to add text listener}Private void setlisteners() {//EditText Control Add text change listenerEt_word_limit.addtextchangedlistener (NewMytextwatcher (Et_word_limit, limit, mainactivity. This)); }}
The above code in the EditText control to add text to listen to, I use a custom textwatcher, need to pass past the parameters of three, the method is:
// editText控件添加文本变化监听 et_word_limit.addTextChangedListener(new MyTextWatcher(et_word_limit, LIMIT, MainActivity.this));
One is the EditText control, which is the edit box control to which you want to add the listener;
One is the limit number of characters, that is, the content that can be entered in the edit box;
One is the context object of the current class.
Of course, if you want to add another TextView control to display the number of characters you enter in real time, then pass one more argument. Anyway, the specific needs, concrete implementation on the line, change is not small, their own learning flexibility.
The most critical class, the custom Textwatcher class, is as follows, with the following code:
PackageCom.example.edittext;ImportAndroid.content.Context;Importandroid.text.Editable;ImportAndroid.text.TextWatcher;ImportAndroid.util.Log;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;/** * Custom Mytextwatcher class implements Textwatcher interface and overrides related methods * * @author Ching * */ Public class mytextwatcher implements textwatcher { Private intLimit//number of characters limit PrivateEditText text;//edit box control PrivateContext context;//Context Object intcursor =0;//To record the position of the cursor when entering characters intBefore_length;//To mark the length of the content in the edit box before entering a content Public Mytextwatcher(EditText text,intLimit, context context) { This. limit = limit; This. Text = text; This. Context = Context; }@Override Public void beforetextchanged(Charsequence S,intStartintCountintAfter) {before_length = S.length (); }/** * s All content in the edit box, the position of the cursor in the start edit box (calculated from 0), count the number of characters entered from the phone's input method * / @Override Public void ontextchanged(Charsequence S,intStartintBefore,intcount) {cursor = start;//LOG.E ("At this point the cursor position is", the cursor + "");}@Override Public void aftertextchanged(Editable s) {//Here you can know the number of words you have entered, you can customize the text control according to the needs of the real-time display of the characters already enteredLOG.E ("At this point you have entered the",""+ s.length ());intAfter_length = S.length ();The total length of all content in the edit box after entering content //If the character has been added beyond the length of the limit, then it is critical that you remove the part that was added later if(After_length > Limit) {//ratio of the maximum number of words beyond the limit intD_value = After_length-limit;//The number of words entered from the phone at this time intD_num = After_length-before_length;intst = cursor + (d_num-d_value);//The start position of the out-of-section to be deleted intEn = cursor + d_num;//The end position of the excess part that needs to be removed //Call the Delete () method to remove the contents of the edit boxEditable s_new = S.delete (St, EN);//Reset Text to edit boxText.settext (S_new.tostring ());//Set the cursor at the end of the display to the beginning of the outside section, optimizing the ExperienceText.setselection (ST);//Popup message tip has exceeded word limitToast.maketext (Context,"Maximum word limit exceeded", Toast.length_short). Show (); } }}
The above code has already given the comment, said is also very clear, the following talk about my idea!
- First, the custom class needs to implement the Textwatcher interface and override the associated method.
At this point, you need to know the edit box before the input content of the number of characters, need to know the input when the cursor location, you need to know the input after the completion of the number of characters in the edit box . These are easy to know, and you know the maximum limit, and you just need to remove some of the characters from the above data. This specific implementation method is written in
public void afterTextChanged(Editable s){}
the
Rewrite the method inside, the key comments are all, we can do a good study, perhaps there will be a more simple method.
In fact, I have not considered the thing, is a character a or a number 1 is counted as a word limit, but in fact this is wrong, so this blog you can refer to and optimize. If you want to use it, it is best to enter the type of content that is single, such as numbers, such as Chinese characters, such as the character. I will continue to solve this problem in the future, to solve the added to my knowledge base, then welcome to read the product.
The following is the demo apk, we have omitted the necessary correctness verification, direct experience, they feel good to reference it! Click I download Test apk
Make a little progress every day! Come on!
Adding Textwatcher monitoring to EditText controls in Android development limits the number of input words