Reputation: 5162
I would like to know how to stop the user from inputting numbers into my edittext programmatically, so that it has a similar effect as doing this would
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#$%......"
I have looked at edittext.setInputType(InputType.something)
but I could not find an input type which stops the user inputting numbers.
Does anyone know how I can do this either through an input type or some other method?
Upvotes: 1
Views: 457
Reputation: 5162
pavki_a's answer worked perfectly, but there were just a few bugs with the backspace and with the word suggestion. I fixed those by adding :
edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
This might be a problem if you want auto correct, but for me it worked fine
Upvotes: 0
Reputation: 507
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String output = "";
for (int i = start; i < end; i++) {
if (!Character.isDigit(source.charAt(i))) {
output += source.charAt(i);
}
}
return output;
}
};
edit.setFilters(new InputFilter[]{filter});
assume edit is your EditText
Upvotes: 3
Reputation: 13254
According to http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType you can use
number - A numeric only field. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_NORMAL.
Have you tried:
edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
Upvotes: 0