Custom Textwatcher: Implement EditText input character limit

Source: Internet
Author: User

As Android developers know, controls such as Textview,edittext have a property android:maxlength= "" to control the length of the text that our controls can enter. However, this length does not distinguish between Chinese and English, such as setting maxlength= "50", at this time can be entered into 50 characters, you can enter 50 data or English. However, in the application, we have input box to the input text length is limited by the character, such as input 100 characters, that requires only 50 Chinese or 100 English. At this point, we can customize the class to inherit Textwatcher, and then call Edittext.addtextchangedlistener (), passing in the corresponding parameters.

-------------------------------------Custom Tool Classes--------------------------------------------

public class Chineseorenglishtextwatcher implements Textwatcher {
/** the edittext*/to be restricted for text input
Private EditText Mfilter;
/** Limit number of characters */
private int length;
/** Space control: Prevents some phones from being in the front when editing information */
Private Boolean onceload = true;
Public Chineseorenglishtextwatcher (EditText mfilter, int length) {
Super ();
This.mfilter = Mfilter;
this.length = length;
}


@Override
public void ontextchanged (charsequence s, int start, int before, int count) {
int countsize = 0;
Editable Editable = Mfilter.gettext ();
String str = editable.tostring ();
char[] array = Str.tochararray ();
int i = 0;
if (onceload) {
Mfilter.setselection (S.length ());
Onceload = false;
}
for (i = 0; i < Array.Length; i++) {
if (char) (BYTE) array[i]! = Array[i])/** determine if it is Chinese */
{
Countsize + = 2;
/** two bytes If it is a Chinese or Chinese special symbol */
} else {
Countsize + = 1;
/** English is a byte */
}
if (countsize > Length) {
/** Reach Maximum */
int selendindex = selection.getselectionend (editable);
str = editable.tostring ();
String newstr = str.substring (0, I);
Mfilter.settext (NEWSTR);
Editable = Mfilter.gettext ();
int newlen = Editable.length ();
if (Selendindex > Newlen) {
Selendindex = Editable.length ();
}
Selection.setselection (editable, selendindex);
Break
}
}


}


@Override
public void beforetextchanged (Charsequence s, int. start, int count, int after) {
String data = s.tostring ();
}


@Override
public void aftertextchanged (Editable s) {}
}

--------------------------and then do the following in the activity you want to use-------------------------------------------------------

public class Testactivity extends activity{

@Override
protected void OnCreate (Bundle savedinstancestate) {
Setcontentview (r.layout.activity_test);

EditText Testedt=findviewbyid (R.id.test_edt);

Testedt.addtextchangedlistener (New Chineseorenglishtextwatcher (testedt,100));//Limit Input 100 characters
}

}

------------------------------------the above custom class to determine whether the Chinese and English characters can also be used to judge the regular expression---------------------------------------------

Regular expressions for judging Chinese:

public static Boolean Ischinesechar (String inputstring) {
Pattern pattern = pattern.compile ("^[\\u4e00-\\u9fa5\\uf900-\\ufa2d]+$");
Return Pattern.matcher (inputstring). matches ();
}

Can put the above:

for (i = 0; i < Array.Length; i++) {
if (char) (BYTE) array[i]! = Array[i])/** determine if it is Chinese */
{
Countsize + = 2;
/** two bytes If it is a Chinese or Chinese special symbol */
} else {
Countsize + = 1;
/** English is a byte */
}

Into:

if (Ischinesechar (Str.charat (i) + ")") {
Count + = 2;
}
if (Filter.indexof (Str.charat (i))! =-1) {
Count + = 1;
}

}

--------------over----------------

Custom Textwatcher: Implement EditText input character limit

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.