-1

How can I do that if the user is not typing A, then there will not be any output? I want to do this in a EditText.

Code:

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.toString(source.charAt(i)).equals("a")) { 
                                    return ""; 
                            } 
                    } 
                    return null; 
            } 
        }; 
Bart Wesselink
  • 286
  • 6
  • 16

1 Answers1

1

You could do it the way that you are attempting to, with an input filter, but I would recommend that you do it in XML instead, by giving your edittext the attribute android:digits="Aa". That should solve your problem.

If you need to use an input filter try this (it is untested and may not work an better than yours)

InputFilter filter = new InputFilter() { 
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
                    String chars = "";
                    for (int i = start; i < end; i++) { 
                            if (Character.toString(source.charAt(i)).equals("a")) { 
                                    chars = chars + source.charAt(i);
                            } else {
                                //don't add anything to the char sequence
                            }
                    } 
                    return chars; 
            } 
        }; 
jcw
  • 5,132
  • 7
  • 45
  • 54