Android edittext Input Word Limit summary (contains Chinese input memory overflow resolution)

Source: Internet
Author: User
Tags gettext


reprint Please specify, big fly:http://blog.csdn.net/rflyee/article/details/38856539

There are many ways to limit the number of edittext input, but the main consideration is two points. That is to deal with two things:
(1) different language characters (English, Chinese, etc.) processing method
(2) If the input character reaches the number, does the user still agree to enter

1th, involved in fact quite a lot of things, different languages in different codes occupy the number of bytes, different languages in the U8, such as the range of coding, and so on, this whole piece of knowledge is very rich, his temporary no reason for the special shun. Let's finish it later.

2nd. At present, the mainstream app processing plans are different, qq5.0 once the version number, published said that seemingly there is no word limit (I tried a 350 words about the same hair), 5.0 after the limit, so that the user input words over the limit will show negative numbers, but still agree to user input, is not published. This gives the processing power to the user, in fact, more convenient!


Realize it.



Method One:
Restrictions in the layout.

Android:maxlength= "  //Limit the maximum number of input characters is 10.
(1) Both Chinese and English are counted as one character.


(2) The number of words can not be entered

Method Two:
Inputfilter restrictions, similar to the restrictions in the layout.

Edittext.setfilters (New Inputfilter[]{new Inputfilter.lengthfilter (10)});
(1) Both Chinese and English are counted as one character.


(2) The number of words to. Cannot enter

Method Three:
Textwatcher restrictions.

This assumes that the word number reaches the limit still agrees that the user input situation is not much to say, good realization.


crash problem : Use Sogou Input method, after the words will or have reached the limit, a large number of Chinese input at a time (do not press space, has been typing. Know that the cache characters of Sogou reach the upper limit). Crash stackoverflow Memory Overflow . Oh, Baidu Input method will not ha.


Solution: The listener in the dynamic removal, join is OK. See Scenario one below

Plan one, Chinese press two, English press one. Number to limit, user cannot enter
Private Textwatcher Textwatcher = new Textwatcher () {private int editstart;    private int editend; private int maxlen = 10; The max Byte@overridepublic void beforetextchanged (charsequence s, int start, int count, int after) {LOG.D ("TextChanged ","---->beforetextchanged:start = "+ Start +" Count = "+ Count +" after = "+ after);} @Overridepublic void OnTextChanged (charsequence s, int start, int before, int count) {} @Overridepublic void Aftertextchang Ed (Editable s) {Editstart = Opinion.getselectionstart (); editend = Opinion.getselectionend ();//Remove the listener first, Otherwise there will be stack Overflow opinion.removetextchangedlistener (textwatcher); Textutils.isempty (Opinion.gettext ())) {String etstring = Opinion.gettext (). toString (). Trim (); while (calculatelength (S.tostring ()) > MaxLen) {s.delete (editStart-1, editend); editstart--;editend--; LOG.D ("TextChanged", "Editstart =" + Editstart + "Editend =" + Editend);}} Opinion.settext (s); opinion.setselection (Editstart);//Recovery Listener Opinion.addtextchangedlistener (Textwatcher);//End by ZYF--------------------------}private int calculatelength (String etstring) {char[] ch = etstring. ToCharArray (); int varlength = 0;for (int i = 0; i < ch.length; i++) {//Changed by ZYF 0825, bug 6918, add Chinese punctuation range, TODO Punctuation range needs to be detailed if ((Ch[i] >= 0x2e80 && ch[i] <= 0xfe4f) | | (Ch[i] >= 0xa13f && ch[i] <= 0xaa40) | | Ch[i] >= 0x80) {//Chinese character range 0x4e00 0x9fbbvarlength = Varlength + 2;} else {varlength++;}} LOG.D ("TextChanged", "varlength =" + varlength);//can also use getBytes, more accurate//Varlength = Etstring.getbytes (charset.f ORName ("GBK")). lenght;//code according to its own needs, note u8 Chinese accounts for 3 bytes ... return varlength;}};


Add:

In fact, the character is out of the case not one by one delete. Instead, the extra characters are all erased at once. You can avoid memory overflow issues, for example, in the following way. (No validation is processed at aftertextchanged.) You can try it yourself, maybe fill it up). In the same way, the solution is only to deal with different ways of interception. (Note: Chinese and English character issues such as the above scheme self-processing)

@Override
Public voidOnTextChanged (Charsequence S,intStartint before,int Count) {
String str = s.tostring ();
if(Length >Maxnum) {
Toastutil.Show(Mcontext,"exceeding the word limit!");
str = str.substring (0,Maxnum);
etcontent. SetText (str);
etcontent. SetSelection (Str.length ());
}
}



Programme II
After limiting the number of words, suppose you enter text or spaces in the middle of the text. The last character will be deleted
Titletv.addtextchangedlistener (New Textwatcher () {@Overridepublic void beforetextchanged (charsequence s, int start, int count, int after) {} @Overridepublic void ontextchanged (charsequence s, int start, int before, int count) {} @Overridepu Blic void aftertextchanged (Editable s) {//Add by ZYF 0825. Superfluous remove from new input, not last Editstart = Opinion.getselectionstart ()   ; Editend = Opinion.getselectionend (); if (! Textutils.isempty (Titletv.gettext ())) {int varlength = 0;int size = 0;  String etstring = Titletv.gettext (). toString (). Trim (); char[] ch = etstring.tochararray (); for (int i = 0; i < ch.length; i++) {size++;if (Ch[i] >= 0x4e00 && ch[i] <= 0x9fbb) {varlength = Varlength + 2;} elsevarlength++;if (Varle Ngth >) {break;}} if (Varlength >) {s.delete (size-1, Etstring.length ());//Add by ZYF 0825. Superfluous remove from new input, not last//S.delete (EDITST Art-1, Editend); Crash StackOverflow, solution to the side scheme one}}});


Reprint please specify, big fly: http://blog.csdn.net/rflyee/article/details/38856539



Android edittext Input Word Limit summary (contains Chinese input memory overflow resolution)

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.