Custom EditText Dynamic Control the number of characters entered

Source: Internet
Author: User

In the development of the inevitable encounter to control the number of characters in the input restrictions, such as the user's name will let you enter 10 characters (meaning that the English can enter 10 but the Chinese characters can only enter 5), perhaps the first reaction in the XML set EditText length, this situation is certainly not up to our requirements. Here's a look at your own approach.

1. Declare a class to inherit inputfilter, implement the method in filter, below the detailed annotation reader can take a closer look at

Public class Mylenghtfilter implements Inputfilter {

int nmax=0;int keep=0;public mylenghtfilter (int nMax) {This.nmax=nmax;}  /** * The following is a personal understanding */@Overridepublic charsequence filter (charsequence source, int start, int end,spanned dest, int dstart, int DEnd) {   //end is the character length to be entered  dstart,dend the default re-entry character two length is the length of the deleted character that you want to wait for    //and  DStart display when the delete character is executed in the input method. DEnd Displays the character length before deletion of try {keep = NMax-(dest.tostring (). GetBytes ("GBK"). Length-(dend-dstart)); if (keep<= 0) {///here to determine the input Whether the character length is greater than or equal to the maximum length return "";} else if (keep >=source.tostring (). GetBytes ("GBK"). Length-start) {//To determine if the input character length is less than the maximum length then returns the input character return null;} else {return source.subsequence (start, (start + Keep)/2), or//if the length of the input is greater than the maximum length, the preceding character output}}catch is truncated ( Unsupportedencodingexception e) {e.printstacktrace ();} return null;}}
2. Custom EditText as follows

public class Myedittext extends EditText {public Myedittext (context context) {super (context);}    public void Setfileter (int maxlen) {this.setfilters (new inputfilter[]{new Mylenghtfilter (MaxLen)}); Public Myedittext (context context, AttributeSet Attrs) {Super (context, attrs), if (attrs!=null) {TypedArray typedarray= Context.obtainstyledattributes (Attrs,r.styleable.myeditinfo); Setfileter (Typedarray.getint ( r.styleable.myeditinfo_maxlen,100)); Typedarray.recycle ();}}}
<resources> <declare-styleable name= "Myautoinfo" > <attr name= "au_background" format= "refere nCE "/> <attr name=" focuseable "format=" boolean "/> <attr name=" textsize "format=" Dimension " ></attr> </declare-styleable></resources>
Custom attributes are used in this class to make it easy to set the length of the input to be controlled in XML.

If there is a better way to read, please tell me the message.

Custom EditText Dynamic Control the number of characters entered

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.