如何使用输入过滤器在Android中一个EditText限制字符?过滤器、如何使用、字符、Android

由网友(帅死的骆驼比马大)分享简介:我要限制的字符为0-9,AZ,AZ只有空格键。设置inputtype我可以限制为数字,但我想不出输入过滤的方式翻翻文档。I want to restrict the chars to 0-9, a-z, A-Z and spacebar only. Setting inputtype I can limit to...

我要限制的字符为0-9,AZ,AZ只有空格键。设置inputtype我可以限制为数字,但我想不出输入过滤的方式翻翻文档。

I want to restrict the chars to 0-9, a-z, A-Z and spacebar only. Setting inputtype I can limit to digits but I cannot figure out the ways of Inputfilter looking through the docs.

推荐答案

我发现这在另一个论坛。工程就像一个冠军。

I found this on another forum. Works like a champ.

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))) {
                return "";
            }
        }
        return null;
    }
};
edit.setFilters(new InputFilter[] { filter });
阅读全文

相关推荐

最新文章