如何过滤EditText的输入? [英] How to filter the input of EditText?
本文介绍了如何过滤EditText的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想过滤EditText
的输入,只允许输入数字和字母,首先我使用TextWatcher
处理最后一个输入字符,但是当您移动光标或将某些内容移到EditText
,此方法失败了,现在我想知道是否有一种方法可以过滤非法输入并向用户提供反馈.
I want to filter the input of an EditText
, only digits and letters are allowed, first I use TextWatcher
to deal with the last input character, but when you move the cursor or you past some content to the EditText
, this method failed, now I want to know is there a way to filter the illegal input and give the user a feedback.
推荐答案
将InputFilter
添加到您的EditText
&为用户提供Toast
. 此代码段将为您提供帮助.
Add InputFilter
to your EditText
& provide a Toast
for user . This code snippet will help you.
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) { // Accept only letter & digits ; otherwise just return
Toast.makeText(context,"Invalid Input",Toast.LENGTH_SHORT).show();
return "";
}
}
return null;
}
};
editText.setFilters(new InputFilter[] { filter });
这篇关于如何过滤EditText的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文