This article describes the method of Android controlling the number of EditText input characters and prohibiting special characters input. Share it for your reference. The specific analysis is as follows:
Here are three methods summarized as follows:
Method 1:
1. Reference two namespaces:
import ; import ;
Used to prohibit special character input control
Define EditText mEditText object
2. Control character length:
Character length control is implemented through InputFilter filtering. The advantage is that dynamic length control can be achieved instead of a length fixed value
int mMaxLenth = 20; 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(()); int destLen = getCharacterNum(()); if (sourceLen + destLen > mMaxLenth) { return ""; } return source; } }; (FilterArray);
3. Special character input is prohibited:
(new TextWatcher() { String tmp = ""; String digits = "/\\:*?<>|\"\n\t"; @Override public void onTextChanged(CharSequence s, int start, int before, int count) { (()); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { tmp = (); } @Override public void afterTextChanged(Editable s) { String str = (); if ((tmp)) { return; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < (); i++) { if (((i)) < 0) { ((i)); } } tmp = (); (tmp); } });
Method 2:
1. Add a reference:
import .*;
2. Define the function:
public static String StringFilter(String str)throws PatternSyntaxException{ String regEx = "[/\\:*?<>|\"\n\t]"; // Characters to be filtered out Pattern p = (regEx); Matcher m = (str); return ("").trim(); }
3. Add listening events:
(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String t = ().toString(); String editable = ().toString(); String str = stringFilter(()); if(!(str)){ (str); (()); //The cursor is set } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } });
Method 3:
int mMaxLenth = 200;//Set the length of characters allowed to enterpublic static String stringFilter(String str)throws PatternSyntaxException{ String regEx = "[/\\:*?<>|\"\n\t]"; Pattern p = (regEx); Matcher m = (str); return (""); } (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 = ().toString(); String str = stringFilter(editable); //Filter special characters if (!(str)) { (str); } (()); cou = (); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { if (cou > mMaxLenth) { selectionEnd = (); (mMaxLenth, selectionEnd); } } });
All the above codes have been tested and shared with everyone! For the first and second, the two methods will have a problem in Samsung NOTE 2 (there are no problems with other phones), and the input window will jump; for the specific reason, interested friends can debug it!
The third method is fine.
I hope this article will be helpful to everyone's Android programming design.