This function is done because when developing a project, since the length of the values of some parameters of the background interface is required and cannot exceed how many characters, the characters entered in the edit box must be limited.
Let’s take a look at the implementation process of the demo:
First, place an EditText control in the xml control, then initialize the control and add text listening to the control. xml is designed simply by yourself. The code is relatively simple, just upload the code:
package ; import ; import ; import ; public class MainActivity extends Activity { private static final int LIMIT = 10;// Maximum word limitprivate EditText et_word_limit;// EditText control@Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); // Get EditText controlet_word_limit = (EditText) findViewById(.editText1); setListeners();// Add text listening in the edit box} private void setListeners() { // editText control adds text change listeninget_word_limit.addTextChangedListener(new MyTextWatcher(et_word_limit, LIMIT, )); } }
When adding text listening to the EditText control in the above code, I used a custom TextWatcher, and there are three parameters that need to be passed, the method is:
// editText control adds text change listeninget_word_limit.addTextChangedListener(new MyTextWatcher(et_word_limit, LIMIT, ));
One is the EditText control, that is, the edit box control to be added to listen to;
One is the limit number of characters, that is, the maximum 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 a TextView control to display the number of characters in real time, just pass another parameter over it. Anyway, the specific needs are enough to realize them, and there are not many changes. Learn to adapt to them yourself.
The most critical class, the custom TextWatcher class is as follows, the code is as follows:
package ; import ; import ; import ; import ; import ; import ; /** * Customize the MyTextWatcher class to implement the TextWatcher interface and rewrite the relevant methods * * @author Zou Qi * */ public class MyTextWatcher implements TextWatcher { private int limit;// Limit the number of charactersprivate EditText text;// Edit box controlprivate Context context;// Context objectint cursor = 0;// Used to record the cursor position when entering charactersint before_length;// Used to mark the length of the content in the edit box before entering a certain contentpublic MyTextWatcher(EditText text, int limit, Context context) { = limit; = text; = context; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { before_length = (); } /** * s All contents in the edit box , start The location of the cursor in the edit box (calculated from 0), count The number of characters entered from the input method of the mobile phone */ @Override public void onTextChanged(CharSequence s, int start, int before, int count) { cursor = start; // ("The cursor position at this time is", cursor + "");} @Override public void afterTextChanged(Editable s) { // Here you can know the number of words you have entered. You can customize the text control to display the number of characters you have entered in real time according to your needs.("You have entered it now", "" + ()); int after_length = ();// The total length of all contents of the edit box after entering the content// If the character added exceeds the limit length, then remove the added part later. This is very importantif (after_length > limit) { // How many words exceed the maximum number of limitsint d_value = after_length - limit; // The number of words entered from the mobile phone at this timeint d_num = after_length - before_length; int st = cursor + (d_num - d_value);// The starting position of the excess part that needs to be deletedint en = cursor + d_num;// The end position of the excess part that needs to be deleted// Call delete() method to remove the contents beyond the edit boxEditable s_new = (st, en); //Reset text for the edit box(s_new.toString()); // Set the last position of the cursor to the starting position of the excess part to optimize the experience(st); // The pop-up message prompts that the word limit has been exceeded(context, "Maximum word limit has been exceeded", Toast.LENGTH_SHORT).show(); } } }
The above code has been commented and it is very clear. Let’s talk about my ideas below!
First of all, this custom class needs to implement the TextWatcher interface and override the related methods.
At this time, you need to know the number of characters in the editing box before the content is entered, you need to know the location of the cursor when entering the content, and you need to know the number of characters in the editing box after the input content is completed. After knowing these, it will be simple. You know the maximum limit number; at this time, you only need to delete the excess characters based on some of the above data. This specific implementation method is written in
public void afterTextChanged(Editable s){}
There are key annotations in the rewriting method. You can study it carefully, and maybe there will be a simpler method.
The above is what the editor introduced to you about adding TextWatcher monitoring to the EditText control in Android development to achieve the limit on the number of input words (recommended). I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!