-3

Canadian postal codes have the following format: A1A1A1, and match the following regex:

[ABCEGHJKLMNPRSTVXY][0-9][A-Z][0-9][A-Z][0-9]

I am setting up an EditText for the user to input their postal code. I would like to display alternatively the text and the number keyboard.

Naively, I use this code in either the TextWatcher or the InputFilter

if (Character.isDigit(s.charAt(s.length() - 1))) {
    zipView.setInputType(InputType.TYPE_CLASS_TEXT
                                    | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
                                    | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
} else {
    zipView.setInputType(InputType.TYPE_CLASS_PHONE);
}

However, this result in an exception when the user inputs the first number.

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:464)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:454)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:33)
        at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
        at android.widget.TextView.doKeyDown(TextView.java:5787)
        at android.widget.TextView.onKeyDown(TextView.java:5600)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2609)

... (continues without ever mentionning my classes.)

My guess is that the input type does not like that there are unsupported characters already in the view.

njzk2
  • 38,969
  • 7
  • 69
  • 107

1 Answers1

0

Input type doesn't actually restrict the keys allowed, its a hint to the keyboard as to what will be entered, so the keyboard can decide to change its UI. Nothing prevents you from adding text to a number field. It looks like a problem calling replace in a filter you set (filters are different from input type).

In fact most keyboards wouldn't even trigger this- XXXKeyListener classes are generally only called by hardware keyboards that cause keydown and keyup events. Software keyboards generally don't do that. Did you give this field a NULL input type initially? That's the only thing that would make the average softkeyboard try to pretend to be a hardware one.

Oh one other thing- setting the input mode while the field is active generally doesn't work well. If you don't reset the input connection, most keyboards won't even detect it (there is no change input type API for the keyboard side). If you do reset the input connection, you're likely to hit all sorts of weird behavior with autocorrect and timing (I know Swype had a dozen different rules for debouncing of events that resetting the input connection interfered with). Its not recommended to change an input type, ever.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I guess so, but those canadian postal code are really anoying to type. there is an initial text input type. Also there is a length inputfilter (6), but I override it and it does not seem to be hit. Thank you for your answer – njzk2 Apr 03 '15 at 18:53