Reputation: 2499
I need to enter only the following characters 0-9
and _
What "android: inputType" in EditText to choose?
Upvotes: 1
Views: 233
Reputation: 11961
If you want to enter only number then choose this android:digits="0123456789_"
.
Upvotes: 0
Reputation: 1803
ok.try the following code
InputFilter[] Textfilters = new InputFilter[1];
Textfilters[0] = new InputFilter(){
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// TODO Auto-generated method stub
if (end > start) {
char[] acceptedChars = new char[]{'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;
}
};
editxt.setFilters(Textfilters);
Upvotes: 0
Reputation: 885
Try and add this to your EditText
EditText text = findViewById(R.id.your_edit_text); /* Find your EditText here. */
text.setKeyListener(DigitsKeyListener.getInstance("0123456789_"));
Upvotes: 0