Edittext - Allow Only Specific Characters Known From Context
In my app i have EditText. I want user to be able to insert only digits and two characters specified by me. I know there exists android:digits='...' but I can't use this because i
Solution 1:
This is the proper way to do such task
InputFilterfilter=newInputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (inti= start; i < end; i++) {
// Your condition here if (!Character.isLetterOrDigit(source.charAt(i))) {
return"";
}
}
returnnull;
}
};
edit.setFilters(newInputFilter[]{filter});
Solution 2:
see my answer here how to create custom InputFilter InputFilter on EditText cause repeating text, of course you have to modify a bit filtering condition
Post a Comment for "Edittext - Allow Only Specific Characters Known From Context"