Android的 - 要限制某些字数到的EditText [英] Android - Want to restrict some charaters to the edittext

查看:89
本文介绍了Android的 - 要限制某些字数到的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困了以下方案。

我有一个编辑的文字和密码字段。我想限制样(包括AZ,az,1-0和一些特殊字符)一些字符。我设置的EditText为textPassword的inputType并用Numberkeylistner限制的字符。它工作正常,在摩托罗拉Xoom平板电脑。

I have an edit text and its a Password field. And I want to restrict some characters like (a-z, A-Z, 1-0 and some special chars). I set the inputType of this edittext as "textPassword" and used Numberkeylistner to restrict the characters. It works fine in Motorola xoom tablet.

不过,我得到的建议,而进入华硕平板电脑的密码。问题是,当我评论以下的套系,然后它的做工精细,这意味着它没有表现出任何建议密码字段。

But I am getting suggestions while entering the Password in ASUS tablet. The problem is when I comment the following set of lines then its working fine that means its not showing any suggestions to the password field.

NumberKeyListener PwdkeyListener = new NumberKeyListener() {

    public int getInputType() {
    return InputType.TYPE_MASK_VARIATION;
    }

    @Override
    protected char[] getAcceptedChars() {
    return new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '@', '.', '_', '#', '$', '%', '&', '*', '-', '+', '(', ')', '!', '"', '\'', ':', 
                    ';', '/', '?', ',', '~', '`', '|', '\\', '^', '<', '>', '{', '}', '[', ']', '=', '£', '¥', '€', '.', '¢', '•','©' };
    }
};

edtObj.setKeyListener(PwdkeyListener);

但我只在华硕平板电脑得到了这个问题。

But I am getting this issue in ASUS tablet only.

谁能告诉我如何限制字符,而无需使用的KeyListener。

Can anyone tell me how to restrict character without using keylistener.

在此先感谢。

推荐答案

最后,我发现这个问题。

Finally I found the issue.

解决方法1:这将解决这个问题,华硕,但显示在MotoXoom数字键盘 在getInputType,而不是返回InputType.TYPE_MASK_VARIATION,我必须回到InputType.TYPE_CLASS_NUMBER。这解决了问题。

Solution 1: Which will solve the issue in ASUS, but showing the number keyboard in MotoXoom In getInputType instead of returning InputType.TYPE_MASK_VARIATION, I have to return InputType.TYPE_CLASS_NUMBER. This solves the issue.

解决方案2:这将解决这一问题的两个。 由于Lukap告诉我修改了CharSequence的方法,这是工作的罚款,并在这里是code。

Solution 2: Which will solve the issue in both. As Lukap told I have modified the CharSequence method and this is working fine and here is the code.

InputFilter[] filters = new InputFilter[1];
    filters[0] = new InputFilter(){
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            if (end > start) {

                char[] acceptedChars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
                        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '@', '.', '_', '#', '$', '%', '&', '*', '-', '+', '(', ')', '!', '"', '\'', ':', 
                        ';', '/', '?', ',', '~', '`', '|', '\\', '^', '<', '>', '{', '}', '[', ']', '=', '£', '¥', '€', '.', '¢', '•','©'};

                for (int index = start; index < end; index++) {                                         
                    if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) { 
                        return ""; 
                    }               
                }
            }
            return null;
        }

    };
    edtTextPassword.setFilters(filters);

这篇关于Android的 - 要限制某些字数到的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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