Recent projects require character type password restrictions, such as not enter the Chinese. EditText now summarize the various implementations, to compare the pros and cons of various methods.
The first way: setting the EditText inputType properties, or can be set by java xml file. If I want to set to display in the form of a password, you can be set up as follows:
In xml, android: inputType = "textPassword"
In java file, can ev.setInputType (InputType.TYPE_TEXT_VARIATION_PASSWORD);
inputType parameters phone, textPasswrod property and so on, are interested can test.
The second: to set the EditText android: digits properties, this approach can point out the characters you want to support. For example, to restrict only numbers and letters, it can be:
android: digits = "0123456789abcdefghijklmnopqrstuvwxyz". PS: Tucao about, written tired, do not support the range operator - or -
Third: Use TextWather monitor changes in the character of EditText, delete unwanted characters when the content changes. It means the user can easily press the keyboard, the code immediately delete illegal characters. PS: the common search keywords is to monitor changes in the input box with TextWatcher, and then check the data and displayed. Sample Code: You can only enter letters or numbers, if you want to support other characters can be modified regEx regular expressions.
= evPwd (the EditText) the findViewById (R.id.ev_password); evPwd.addTextChangedListener (new new TextWatcher () { @Override public void beforeTextChanged (CharSequence CharSequence, I int, int I1, I2 int) { } @Override public void OnTextChanged (CharSequence CharSequence, I int, int I1, I2 int) { String = evPwd.getText the editable () toString ();. String regEx = "[^ A-zA-Z0-9]"; // only letters or numbers Pattern of Pattern.compile = P (regEx); Matcher m = p.matcher (the editable); String m.replaceAll STR = ( "") TRIM ();. // not delete a letter or number characters if (editable.equals! (STR)) { evPwd.setText (STR); // set of characters EditText evPwd.setSelection (str.length ()); // delete the character because, to be rewritten to set the new location of the cursor } }
Fourth: to achieve by InputFilter. Achieve InputFilter filter you need to cover a method called the filter.
abstract CharSequence filter public (
CharSequence Source, enter text //
int start, // start position
int end, // end position
Spanned dest, // content currently displayed
int dstart, // current starting position
int dend // end of the current position
);
Note: IntentFilter is an array, which means you can write more filters!
The following implementation enables EditText receive only the characters (numbers, letters), Character.isLetterOrDigit will also be used as Chinese Letter, so you want to write a regular judge whether Chinese.
evPwd.setFilters(new InputFilter[]{ new InputFilter() { @Override public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) { String regex = "^[\u4E00-\u9FA5]+$"; boolean isChinese = Pattern.matches(regex, charSequence.toString()); if (!Character.isLetterOrDigit(charSequence.charAt(i)) || isChinese) { return ""; } return null; } } });
Fifth: Use the EditText InputConnection property limit input characters. New classes inherits EditText onCreateInputConnection and covering function, replace EditText LimitText used in the xml.
public class LimitEditText extends EditText { public LimitEditText(Context context) { super(context); } public LimitEditText(Context context, AttributeSet attrs) { super(context, attrs); } public LimitEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 输入法 * @param outAttrs * @return */ @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new InnerInputConnecttion(super.onCreateInputConnection(outAttrs), false); } } the extends InputConnectionWrapper the implements the InputConnection InnerInputConnecttion class { public mInputConnecttion (the InputConnection target, the mutable Boolean) { Super (target, the mutable); } / ** * the content of the input intercept * * @param text * @param newCursorPosition * @return * / @ override public Boolean commitText (CharSequence text, int newCursorPosition) { // only letters or numbers IF {(Character.isLetterOrDigit (charSequence.charAt (I)) || isChinese!) return to false; return super.commitText(text, newCursorPosition); } @Override public boolean sendKeyEvent(KeyEvent event) { return super.sendKeyEvent(event); } @Override public boolean setSelection(int start, int end) { return super.setSelection(start, end); } } }
These are all methods EditText input limit, if missing please add ~ ~ ~