问题
I got stuck up with the following scenario.
I have an edit text and its a Password field. And I want to restrict some characters like (a-z, A-Z, 1-0 and some special chars). I set the inputType of this edittext as "textPassword" and used Numberkeylistner to restrict the characters. It works fine in Motorola xoom tablet.
But I am getting suggestions while entering the Password in ASUS tablet. The problem is when I comment the following set of lines then its working fine that means its not showing any suggestions to the password field.
NumberKeyListener PwdkeyListener = new NumberKeyListener() {
public int getInputType() {
return InputType.TYPE_MASK_VARIATION;
}
@Override
protected char[] getAcceptedChars() {
return new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '@', '.', '_', '#', '$', '%', '&', '*', '-', '+', '(', ')', '!', '"', '\'', ':',
';', '/', '?', ',', '~', '`', '|', '\\', '^', '<', '>', '{', '}', '[', ']', '=', '£', '¥', '€', '.', '¢', '•','©' };
}
};
edtObj.setKeyListener(PwdkeyListener);
But I am getting this issue in ASUS tablet only.
Can anyone tell me how to restrict character without using keylistener.
Thanks in advance.
回答1:
The Input FIlter could be used for restricting the type of charcters being entered to an editText field. In this code, Character.isLetterOrDigit(...) is used to avoid letters & digits. Using Character.getType(..), the type of each charcter being enterd could be found. Apply your logic here to meet your requirement.
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
System.out.println("Type : "+Character.getType(source.charAt(i)));
if (Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
editText.setFilters(new InputFilter[] { filter });
Hope this would help.
回答2:
Finally I found the issue.
Solution 1: Which will solve the issue in ASUS, but showing the number keyboard in MotoXoom In getInputType instead of returning InputType.TYPE_MASK_VARIATION, I have to return InputType.TYPE_CLASS_NUMBER. This solves the issue.
Solution 2: Which will solve the issue in both. As Lukap told I have modified the CharSequence method and this is working fine and here is the code.
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter(){
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start) {
char[] acceptedChars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '@', '.', '_', '#', '$', '%', '&', '*', '-', '+', '(', ')', '!', '"', '\'', ':',
';', '/', '?', ',', '~', '`', '|', '\\', '^', '<', '>', '{', '}', '[', ']', '=', '£', '¥', '€', '.', '¢', '•','©'};
for (int index = start; index < end; index++) {
if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) {
return "";
}
}
}
return null;
}
};
edtTextPassword.setFilters(filters);
回答3:
I think you should see this: http://developer.android.com/reference/android/text/InputFilter.html; and this have similar problem solved.
Sometimes when you have this kind of issues you do the folowing:
if(motorola){
//stuff that works for motorola
}else if(asus){
//stuff that works for asus
}
Note: that sometimes there is not one solution that works everywhere .
来源:https://stackoverflow.com/questions/7926896/android-want-to-restrict-some-charaters-to-the-edittext