In Android development, add Textwatcher to EditText controls to implement restrictions on input words (recommended) _android

Source: Internet
Author: User

This function is due to the development of the project, because the background interface some parameters of the value of the length of the request, can not exceed the number of characters, so the edit box to enter the characters are limited.

Here is a look at the demo implementation process:

First, you place a EditText control in the XML control, and then initialize the control and add text listening to the control. XML itself simple design, the code is simpler, directly on the code:

Package com.example.edittext;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.EditText;
public class Mainactivity extends activity {
private static final int LIMIT = 10;//maximum word limit
private edittext et_wor d_limit;//edittext Control
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate ( Savedinstancestate);
Setcontentview (r.layout.activity_main);
Gets the EditText control
et_word_limit = (edittext) Findviewbyid (R.ID.EDITTEXT1);
Setlisteners ();//edit box Add text Listener
}
private void Setlisteners () {
//EditText Control Add text change monitor
Et_word_ Limit.addtextchangedlistener (New Mytextwatcher (Et_word_limit, limit, mainactivity.this));
}

In the code above to add text listening to the EditText control, I use a custom textwatcher, there are three parameters that need to pass past, the method is:

EditText control adds text change listening
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 a listener;

One is the limit number of characters, that is, the maximum 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 in real time to display the number of characters entered, then pass another parameter to the past. Anyway, specific requirements, concrete implementation on the line, change is not small, learn to adapt.

The most critical class, the custom Textwatcher class, has the following code:

Package com.example.edittext;
Import Android.content.Context;
Import android.text.Editable;
Import Android.text.TextWatcher;
Import Android.util.Log;
Import Android.widget.EditText;
Import Android.widget.Toast; /** * Custom Mytextwatcher class implements Textwatcher interface, and overrides related method * * @author Ching * * * * * */public class Mytextwatcher implements {PR ivate int limit;//number of characters limit private edittext text;//edit box control Private context context;//Contextual object int cursor = 0;//used to record input characters when the cursor Position int before_length;//The length of the content in the edit box before you enter a content public mytextwatcher (edittext text, int limit, context-context) {This.lim
it = limit;
This.text = text;
This.context = context; 
@Override public void beforetextchanged (charsequence s, int start, int count, int after) {before_length = S.length ();}  /** * s edit box all content, the position of the cursor in the start 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;//LOG.E ("At this point the cursor position 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 according to the needs of the real-time display of the characters have been entered LOG.E ("At this time you have entered", "" + s.length ()); int after_length = s.length ()//The total length of all contents of the edit box after input//if the character is added beyond the limit length, then remove the part that is added later, which is critical if (After_length > limit) {/
/ratio limit maximum number of words int d_value = After_length-limit;
At this time from the phone input number of words int d_num = after_length-before_length; int st = cursor + (d_num-d_value)//The start position of the part that needs to be removed int en = cursor + d_num;//The end position of the excess part to be deleted/Call the Delete () method to add the inside of the edit box to the outside part
Tolerance Remove Editable s_new = S.delete (St, EN);
Reset the text to the edit box Text.settext (S_new.tostring ());
Set the position where the cursor is last displayed as the starting position beyond the section and optimize the experience text.setselection (ST);
Pop-up message hint has exceeded the word limit toast.maketext (context, "has exceeded the maximum word limit", toast.length_short). Show (); }
}
}

The above code has given the annotation, said also very clear, below said my idea!

First, the custom class needs to implement the Textwatcher interface and rewrite the associated method.

At this point, you need to know the edit box before the input of the number of characters, you need to know the location of the cursor when the input content, you need to know the contents of the edit box after the number of characters. This is easy to know, the maximum limit you know; At this time only to be based on some of the above data to delete the above part of the character. This specific implementation method is written in the

 
 

Rewrite the method inside, the key notes are there, we can study it, perhaps there will be a more simple way.

The above is a small set to introduce the Android development to EditText control to add textwatcher Monitor to achieve the input word limit (recommended), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.