Edittext inputtype constant value doesnot match

时光怂恿深爱的人放手 提交于 2019-12-01 20:57:30

问题


In android xml file im using editext as

<EditText
  android:id="@+id/email"
  android:layout_width="fill_parent"
  android:layout_height="33dp"
  android:inputType="textEmailAddress"
  android:hint="Enter your mail id" />

In java file while validating that editext.

if(editextobj.getInputType()==InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS){

}

or

if(getInputType()==(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS)){

}

this condition is not working since editextobj.getInputType() returns 33 whereas developer document gives TYPE_TEXT_VARIATION_EMAIL_ADDRESS constant value as 32

How to validate inputype programatically?


回答1:


There is nothing wrong with your code. 32 stays for TYPE_TEXT_VARIATION_EMAIL_ADDRESS. Also it's a flag, so you should test it like this. See InputType example(at the top under class overview) for more details.

if(editextobj.getInputType() & InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS == 1){

}



回答2:


You need to test each flag separately, e.g.:

if ( ( editextobj.getInputType() & InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS) != 0)
{
     // This is an email address!
}



回答3:


The following is happening:
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS value is: 32.
InputType.TYPE_CLASS_TEXT value is: 1.

the (|) is a bitwise OR operation. It's doing modification at the binary level:

Decimal: 32|1 results in 33
Binary: 100000|1 results in 100001 which is 33 in decimal.

editextobj.getInputType() value is 33




回答4:


Try this:

if(editextobj.getInputType() == (InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS + 1)) {....}


来源:https://stackoverflow.com/questions/20781087/edittext-inputtype-constant-value-doesnot-match

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