问题
I want to make an EditText
box and use TextWatcher
to prevent typing of special characters.
回答1:
This is for only character and space validation...
InputFilter filter = new InputFilter()
{
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
{
for (int i = start; i < end; i++)
if (!Character.isLetter(source.charAt(i)) && !Character.isSpaceChar(source.charAt(i)))
return "";
return null;
}
};
myTextView.setFilters(new InputFilter[] { filter });
来源:https://stackoverflow.com/questions/11179158/android-use-text-watcher-to-prevent-typing-of-special-characters