Why does CLASS_TYPE_PHONE allow non-phone characters in Android?

232 views Asked by At

I have a custom view which contains an EditText. Using my own custom view, I'm using this:

mCustomView.setInputType(InputType.TYPE_CLASS_PHONE);

and in the custom view class we have:

public void setInputType(int mode){
    mSearchEditText.setInputType(mode);
}

However, the keyboard has these:

keyboard

All I want is the numbers and the + sign. No -, no N, to -, no /.

2

There are 2 answers

2
CommonsWare On BEST ANSWER

There are ~2 billion Android devices, from thousands of device models. These will ship with dozens, if not hundreds, of pre-installed input method editors (a.k.a., soft keyboards). There are other input method editors available for download from the Play Store and elsewhere.

InputType.TYPE_CLASS_PHONE is a hint or request that you are making to the input method editor. It is not a command. What the input method editor does, in response to InputType.TYPE_CLASS_PHONE, is up to the developers of the input method editor, not you. And, since there are lots of different input method editors, the results will vary by user.

So, for this particular input method editor, you get what you show in the screenshot. While I do not recognize a couple of those with respect to telephony (N?), most of them are routinely used with phone numbers (e.g., parentheses to separate out area codes in the United States). Regardless, it is the developers of that input method editor who choose what appears as options, not you.

0
David Wasser On

Setting the input type just determines which keyboard type will be shown (as default). To limit the input to only digits, use:

android:digits="0123456789"

in the <EditText> definition.

Note: There is no standard Android keyboard type that shows only digits!