Android edittext Input Word limit summary (including Chinese input memory overflow resolution)

Source: Internet
Author: User


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

1th, the east is actually quite a lot of things, different languages in different codes occupy the number of bytes, different languages in the U8, such as the expression range of coding, this whole piece of knowledge is very rich,

I have no reason for the special shun, and later finishing it.

2nd, the current Mainstream app processing program also has different, qq5.0 previous version, published said that seemingly there is no word limit (I tried a 350 words about the same hair),

5.0 later limit, so if the user input word number exceeds the limit will show negative numbers, but still allow user input, is not published. This gives the user all the processing power,

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 can not be entered

Method Three:
Textwatcher restrictions. Here if the word limit still allows the user to enter the situation is not much to say, good implementation.

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 Sogou cache characters to reach the upper limit),

Crash,stackoverflow memory overflow. Oh, Baidu Input method will not ha.

Solution: The listener in the dynamic removal, add 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 to be materialized 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 be used here 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;}};


Programme II
After the limit word is reached, if you enter text or a space 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 ();   


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



Android edittext Input Word limit summary (including 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.