如何使用输入过滤器在Android中一个EditText限制字符? [英] How do I use InputFilter to limit characters in an EditText in Android?

查看:188
本文介绍了如何使用输入过滤器在Android中一个EditText限制字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要限制的字符为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 });

这篇关于如何使用输入过滤器在Android中一个EditText限制字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆