Android Get EditText Input Type At Runtime

前端 未结 3 806
星月不相逢
星月不相逢 2021-01-05 13:48

I have a custom implementation of an edit text class.

Based on these XML attribute.....

android:inputType=\"textPersonName\"
android:inputType=\"text         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-05 14:22

    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!

提交回复
热议问题