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.