android edittext获取 time值,Android Get EditText Input Type At Runtime

问题

I have a custom implementation of an edit text class.

Based on these XML attribute.....

android:inputType="textPersonName"

android:inputType="textPersonName"

android:inputType="textEmailAddress"

android:inputType="textPassword"

I want to be able to know this at run time (In Java Code)

I have found this method which gives me the input type

getInputType()

And these are the values returned based on the XML I posted above.

97,97,33,129

However, these do not correspond to the constant values listed here

http://developer.android.com/reference/android/text/InputType.html

How can I know the input type of an edit text at run time?

回答1:

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.

回答2:

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

Setting the InputType

回答3:

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!

来源:https://stackoverflow.com/questions/32477817/android-get-edittext-input-type-at-runtime

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值