In Android development, special character filtering and character length restrictions in edittext are frequently required. Due to compatibility issues between different Android versions and different mobile phone support situations, some device incompatibility problems often occur. To solve this problem, we have summarized the practice and provided an optimal solution:
First, we can use the following three methods to limit the length of characters:
1. Use the setfilter method of edittext. The Code is as follows:
Defines the edittext object medittext;
Character limit length int max_text_input_length;
Medittext. setfilters (New inputfilter [] {New inputfilter. lengthfilter (max_text_input_length )});
This method can only limit the number of characters with a fixed length, that is, max_text_input_length must be a fixed value.
2. Use the setfilters method to dynamically change the character length limit:
Int mmaxlenth = 20; // mmaxlenth can be dynamically changed
Inputfilter [] filterarray = new inputfilter [1];
Filterarray [0] = new inputfilter (){
@ Override
Public charsequence filter (charsequence source, int start, int end,
Spanned DEST, int dstart, int dend ){
Boolean binvlid = false;
Int sourcelen = getcharacternum (source. tostring ());
Int destlen = getcharacternum (DEST. tostring ());
If (sourcelen + destlen> mmaxlenth ){
Return "";}
Return source;
}
};
Medittext. setfilters (filterarray );
3. Use addtextchangedlistener of edittext to listen to events:
Medittext. addtextchangedlistener (New textwatcher (){
Private int cou = 0;
Int selectionend = 0;
@ Override
Public void ontextchanged (charsequence S, int start, int before,
Int count ){
Cou = before + count;
String editable = medittext. gettext (). tostring ();
Medittext. settext (editable );
}
Medittext. setselection (medittext. Length ());
Cou = medittext. Length ();
}
@ Override
Public void beforetextchanged (charsequence S, int start, int count,
Int after ){
}
@ Override
Public void aftertextchanged (editable s ){
If (COU> mmaxlenth ){
Selectionend = medittext. getselectionend ();
S. Delete (mmaxlenth, selectionend );
}
}
});
Test the above three methods on different mobile phones and find the following problems:
If two methods are used: Android 4.0 and earlier versions, the English input method of the Samsung keyboard may be abnormal, such as Samsung S1, for mobile phones of Versions later than 4.0, the English input method of Samsung keyboard is also used, and the input character is incorrect. For example, Samsung Note 2 contains many more characters in the input box.
For the 3rd methods, the performance is relatively stable.
For character filtering, similarly, if we use the setfilters method, there will still be problems with the input box beating and inexplicable many more characters.
After testing by Samsung, LG, Google, and other brands of mobile phones, we finally provide an optimal solution, that is, to filter characters and limit the length in the addtextchangedlistener listening event of edittext:
// Set the character filtering function (to filter out unnecessary characters)
Public
StaticString stringfilter (string Str)ThrowsPatternsyntaxexception {
String RegEx =
"[/\\:*? <> | \ "\ N \ t]";
Pattern P = pattern.Compile(RegEx );
Matcher M = P. matcher (STR );
ReturnM. replaceall ("");
}
Int mmaxlenth = 50;
Medittext. addtextchangedlistener (NewTextwatcher (){
Private
IntCou = 0;
Int
Selectionend = 0;
@ Override
Public
VoidOntextchanged (charsequence s,
IntStart,IntBefore,
IntCount ){
Cou = before + count;
String editable =
Medittext. gettext (). tostring ();
String STR =
Stringfilter(Editable );
If(! Editable. Equals (STR )){
Medittext. settext (STR );
}
Medittext. setselection (medittext. Length ());
Cou =
Medittext. Length ();
}
@ Override
Public
VoidBeforetextchanged (charsequence s,
IntStart,IntCount,
IntAfter ){
}
@ Override
Public
VoidAftertextchanged (editable s ){
If(COU>
Mmaxlenth ){
Selectionend =
Medittext. getselectionend ();
S. Delete (mmaxlenth,
Selectionend );
If(Androidversion. charat (0)> = '4 ')
{
Medittext. settext (S. tostring ());
}
}
}
};