3

I got stuck up with the following scenario.

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.

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

Thanks in advance.

Padma
  • 656
  • 3
  • 11
  • 30

3 Answers3

11

The Input FIlter could be used for restricting the type of charcters being entered to an editText field. In this code, Character.isLetterOrDigit(...) is used to avoid letters & digits. Using Character.getType(..), the type of each charcter being enterd could be found. Apply your logic here to meet your requirement.

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++) {
            System.out.println("Type : "+Character.getType(source.charAt(i)));

            if (Character.isLetterOrDigit(source.charAt(i))) {
            return "";
            }           
        }

        return null;
        }
    };

    editText.setFilters(new InputFilter[] { filter });

Hope this would help.

Kannan Suresh
  • 4,573
  • 3
  • 34
  • 59
4

Finally I found the issue.

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.

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);
Padma
  • 656
  • 3
  • 11
  • 30
1

I think you should see this: http://developer.android.com/reference/android/text/InputFilter.html; and this have similar problem solved.

Sometimes when you have this kind of issues you do the folowing:

if(motorola){
//stuff that works for motorola
}else if(asus){
//stuff that works for asus
}

Note: that sometimes there is not one solution that works everywhere .

Community
  • 1
  • 1
Lukap
  • 31,523
  • 64
  • 157
  • 244
  • Hi Lukap, thanks for your reply, Yes I have seen the following links related to input filters http://blog.mobile-j.de/2009/12/i-really-like-androids-edittext.html and http://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext-in-android But I am not sure how to restrict my char array using these samples. – Padma Oct 28 '11 at 08:48
  • you need to implement public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) to fit your purpose, play with it a bit and you will get it how it works – Lukap Oct 28 '11 at 08:53
  • yes you are right, I thought of avoiding the for loop to check inside that CharSequesnce method. – Padma Oct 28 '11 at 11:20