Optimal method for special character filter and character length limit of EditText in Android

In the development of Android, often encountered in the special character of EditText filter and character length limit at the same time, due to the problem of different versions of Android compatibility issues, as well as a variety of mobile phone support is different, therefore, often appear some devices are not compatible with the above questions. In order to solve this problem, this problem is summarized, and an optimal scheme is given:

First, we can use 3 methods for the limit of the length of the character, as follows:

1, using the setFilter EditText method to achieve, the code is as follows:

Define EditText object mEditText;

Character limit MAX_TEXT_INPUT_LENGTH int;

MEditText.setFilters (InputFilter[]{new InputFilter.LengthFilter new (MAX_TEXT_INPUT_LENGTH)});

This method can only limit the number of characters to a fixed length, that is, MAX_TEXT_INPUT_LENGTH must be a constant value.

2, the same is the use of setFilters method, the dynamic change of the limit of the character length:

MMaxLenth int = 20; //mMaxLenth can be dynamically changed

FilterArray new = InputFilter[1] InputFilter[];
FilterArray[0] = InputFilter new () {
CharSequence filter CharSequence (source int, start int, end public,
Dest int, dstart int, dend Spanned) {
BInvlid Boolean = false;
SourceLen int = getCharacterNum (source.toString ());
DestLen int = getCharacterNum (dest.toString ());
If (sourceLen + destLen > mMaxLenth) {
Source return;

MEditText.setFilters (FilterArray);

3 using addTextChangedListener EditText monitor event:

MEditText.addTextChangedListener (TextWatcher new) {
Int cou private = 0;
SelectionEnd int = 0;

Void onTextChanged CharSequence (s int, start int, before public,
Count int) {
Cou = count + before;
Editable String = mEditText.getText ().ToString ();
MEditText.setText (editable);
MEditText.setSelection (mEditText.length ());
Cou = mEditText.length ();

Void beforeTextChanged CharSequence (s int, start int, count public,
After int) {

Void afterTextChanged public (s Editable) {
If (cou > mMaxLenth) {
SelectionEnd = mEditText.getSelectionEnd ();
S.delete (mMaxLenth, selectionEnd);

For these three methods, the following problems are found by testing different types of mobile phones:

If you use 2, two ways, in the version of Android 4, the Samsung keyboard English input method, the input box will appear next to the next jump problem, such as Samsung S1, and for more than 4 version of the phone, the same is the Samsung keyboard English input method, the input word error, such as: Samsung NOTE 2 above in the input box will be inexplicable many characters.

For the third methods, the performance is relatively stable.

For character filtering, in the same way, if we use the setFilters method to achieve, then, there will still be a lot of input box beat and a lot of characters.

After Samsung, LG, Google and other brands of mobile phone testing, and finally gives an optimal solution, that is, in the addTextChangedListener EditText monitor to achieve the character filter and length limit:

/ /Set filter character function(Filter out the characters we don't need.)

Public StaticStringFilter String (STR String)ThrowsPatternSyntaxException{

       RegEx String ="[/\\: * \n\t] <>|\?"";

       P Pattern = Pattern.Compile(regEx);

       M Matcher = p.matcher (STR);

       ReturnM.replaceAll ("");


MMaxLenth int = 50;

MEditText.addTextChangedListener (NewTextWatcher () {

           Private Int Cou= 0;

           Int SelectionEnd= 0;


           Public VoidOnTextChanged (s CharSequence,IntStart,IntBefore,

                   IntCount) {

               Cou= before + count;

               Editable String =MEditText.getText ().ToString ();

               STR String =StringFilter(editable);

               If(editable.equals (STR)) {

                   MEditText.setText (STR);


               MEditText.setSelection (MEditText.length ());

               Cou=MEditText.length ();



           Public VoidBeforeTextChanged (s CharSequence,IntStart,IntCount,

                   IntAfter) {



           Public VoidAfterTextChanged (s Editable) {


                   SelectionEnd=MEditText.getSelectionEnd ();

                   S.delete (MMaxLenth,SelectionEnd);

                   If(AndroidVersion.charAt (> = 0)'4')


                       MEditText.setText (s.toString ());





For more complete information about compiler optimizations, seeOptimized notification.