我只需要输入以下字符0-9
和_
EditText 中选择什么“android: inputType”?
就这样做XML
android:digits="0123456789_"
尝试将此添加到您的EditText
EditText text = findViewById(R.id.your_edit_text); /* Find your EditText here. */
text.setKeyListener(DigitsKeyListener.getInstance("0123456789_"));
如果您只想输入数字,请选择此项android:digits="0123456789_"
。
好的。试试下面的代码
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);