Edittext set for password with phone number input?

2019-01-12 02:02发布

问题:

How do I get a Edittext with both a phone input and the ability to hide the string. I know that

android:inputType="textPassword"

hides the string, while

android:inputType="phone"

brings up a dialpad interface.

How to combine the two?

回答1:

android:password is deprecated, but AFAIK is the only way because android:inputType="phone|textPassword" is ignored ...

<EditText
    android:id="@+id/EditText01"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:password="true"
    android:inputType="phone" />


回答2:

I believe this is what you want?

android:inputType="numberPassword"

Edit: At the time of the question (2010) this may have not been in the API, but for contemporary development, it's available.



回答3:

I have not found an appropriate solution to this problem. dtmilano's accepted solution doesn't fully work. If the EditText is focused in landscape mode where you have the full screen keyboard, the numbers still display in clear text, not masked.

I spent significant time going through the actual TextView code, and the reason this is a problem is that they are explicitly checking the InputType against InputType.TYPE_CLASS_TEXT and, if I recall correctly, TYPE_MASK_CLASS. So if you include any other InputType within those bounds (I think the range used by TYPE_CLASS_TEXT and TYPE_MASK_CLASS is the first byte), then it won't be recognized as a password that needs masking.

I know what I said is pretty confusing. The actual code is a LOT more confusing. I was pretty appalled at the TextView's code to be honest. It's a tangled mess, with hard coded checks everywhere. Horrible coding practice which leads to problems like this.



回答4:

This problem can be solved without using deprecated android:password. See my answer here.



回答5:

I haven't tried this, but it might be possible to combine the two like so:

android:inputType="textPassword|phone"

since inputType can take on multiple values.