Android Get EditText Input Type At Runtime

陌路散爱 提交于 2019-11-30 22:17:40

The numbers you are getting are decimal numbers.For example textPassword has constant value 0x00000081 in hex.When you convert that in decimal it will give 129.

Hence the output you are getting is perfect.

Refer this to find the list of all input-types with their hex values.

How to get the InputType from an EditText

To get the input type use getInputType().

int inputTypeValue = editText.getInputType();

The InputType values are defined (in hexadecimal) in the documentation.

You can test the values with something like

if (inputTypeValue == InputType.TYPE_CLASS_TEXT) { ... }

See also

Please see this link. You see that the exact number you get is by 'OR'ing the two Constants:

https://developer.android.com/reference/android/widget/TextView#attr_android%3AinputType

like to get the value of textEmailAddress, [OR] InputType.TYPE_CLASS_TEXT with InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,

like this: InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS.

Please see the code:

String strInputType;
            final int inputType = editText.getInputType();
            switch (inputType) {
                case (InputType.TYPE_TEXT_FLAG_CAP_WORDS|InputType.TYPE_CLASS_TEXT): {
                    strInputType = "Name ";
                }
                break;
                case (InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT): {
                    strInputType = "Password or Confirm Password ";
                }
                break;
                case (InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS|InputType.TYPE_CLASS_TEXT): {
                    strInputType = "Email ";
                }
                break;
                case InputType.TYPE_CLASS_PHONE: {
                    strInputType = "Phone Number ";
                }
                break;
                case InputType.TYPE_CLASS_DATETIME: {
                    strInputType = "Date ";
                }
                break;
                case InputType.TYPE_CLASS_NUMBER: {
                    strInputType = "Number ";
                }
                break;
                case InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS: {
                    strInputType = "Address ";
                }
                break;
                default: {
                    strInputType = "Field ";
                }
                break;
            }
            Resources res = baseActivity.getResources();
            String message = res.getString(R.string.field_blank, strInputType);

Happy Coding!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!