过滤限制EditText文本输入
限制接收的字符
通过setKeyListener限制能接收的字符
edt.setKeyListener(new NumberKeyListener() {
@NonNull
@Override
protected char[] getAcceptedChars() {
return new char[]{'1','2','3','4','5'};//能接收的字符数组
}
@Override
public int getInputType() {
return InputType.TYPE_CLASS_TEXT;//返回键盘类型
}
});
过滤字符
通过InputFilter过滤输入的字符
//下一个字符和前一个不能重复
InputFilter inputFilter = new InputFilter(){
private String lastStr = "";
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
Log.d(TAG, "filter: --"+source.toString());
if (lastStr.equals(source.toString())){
return "";
}
lastStr = source.toString();
return null;//返回原始值
}
};
edt.setFilters(new InputFilter[]{inputFilter});