Summary of Android EditText input word limit (including solutions for Chinese Input memory overflow) and androidedittext

Source: Internet
Author: User

Summary of Android EditText input word limit (including solutions for Chinese Input memory overflow) and androidedittext

There are many solutions to limit the number of EditText inputs, but generally there are two main considerations:
(1) Handling Methods of characters in different languages (English and Chinese)
(2) Whether user input is allowed after the number of characters is reached

First, there are actually a lot of things involved. Different Languages occupy the number of bytes in different encodings, and different languages have the U8 encoding representation range,

I am not rational at the moment. I will sort it out later.

Second, the current mainstream app processing solutions are also different. In versions earlier than qq5.0, it seems that there is no limit on the number of words (I tried a 350 word or so ),

If the number of words entered by the user exceeds the limit, a negative number is displayed. However, if the number of words entered by the user exceeds the limit, the user is still allowed to enter the number, and the table cannot be published. In this way, all the processing permissions are handed over to the user,

It's actually easier!

.

Method 1:
Layout restrictions.
Android: maxLength = "10" // The maximum number of input characters is 10.
(1) Both Chinese and English are considered a single character.
(2) No words can be entered

Method 2:
InputFilter restrictions, similar to those in layout.
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
(1) Both Chinese and English are considered a single character.
(2) No words can be entered

Method 3:
TextWatcher restrictions. Here, if the number of words reaches the limit, users are not allowed to enter it.

Crash problem: when the sogou input method is used, a large number of Chinese characters are entered at a time after the number of words is about or has reached the limit (without spaces, typing is always done, knowing that sogou's cache characters have reached the limit ),

Crash and stackoverflow memory overflow. Haha, Baidu's input method won't.

Solution: Remove and add the listener dynamically. See solution 1 below

Solution 1: either Chinese or English. Number to limit. You 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, I Nt before, int count) {}@ Overridepublic void afterTextChanged (Editable s) {editStart = opinion. getSelectionStart (); editEnd = opinion. getSelectionEnd (); // remove the listener first; otherwise, the stack overflow opinion may occur. removeTextChangedListener (textWatcher); if (! 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); // restores the opinion listener. 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, added to the Chinese Punctuation range, 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); // getBytes can be used here, which is more accurate. // varlength = etstring. getBytes (CharSet. forName ("GBK ")). lenght; // encoding according to your needs. Note that u8 occupies 3 bytes of Chinese... return varlength ;}};


Solution 2
// When the limit is reached, if a text or space is entered 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) {}@ Overridepublic void afterTextChanged (Editable s) {// add by zyf 0825. the redundant location is deleted from the new input, instead of the 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 (varlength> 80) {break ;}} if (varlength> 80) {s. delete (size-1, etstring. length (); // add byzyf 0825. the redundant location is deleted from the new input, instead of the last // s. delete (editStart-1, editEnd); // crash stackoverflow, solution 1 }}}});


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






Related Article

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.