Difference between android:inputType=“textPassword

2019-01-24 00:39发布

问题:

Can anyone explain the differences between the

   android:inputType="textPassword",
   android:inputType="textVisiblePassword",
   android:inputType="textWebPassword",
   android:inputType="numberPassword"

of EditText ViewGroup in Android Layout?

回答1:

From the doc for android:inputType:

textPassword
Value: 0x00000081
Text that is a password. Corresponds to
TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD.

textVisiblePassword
Value: 0x00000091
Text that is a password that should be visible. Corresponds to
TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD.

textWebPassword
Value: 0x000000e1
Text that will be used as a password on a web form. Corresponds to
TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_PASSWORD.

numberPassword
Value: 0x00000012
A numeric password field. Corresponds to
TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD.



回答2:

android:inputType="textPassword"

The attribute above will take the password as a string.

android:inputType="textVisiblePassword"

The one above will make the password text visible.

android:inputType="numberPassword"

And this one will take a numeric password only.



回答3:

Even though it has been already answered I'll add some more details to the differences in password InputType variations:

  1. android:inputType="textPassword": Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD i.e. it allows you to enter a string that is taken as a password (hidden and preventing autocompletion and suggestions unless set explicitly). This one is mostly used when we want to enter passwords.
  2. android:inputType="textVisiblePassword": Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD and is the same as the previous one but the password is visible (useful if you want to use it to allow to see the password as default because it prevents the autocompletion and suggestions unless they are set explicitly - it is advisable to also have a way to hide the password)
  3. android:inputType="numberPassword": Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD same as android:inputType="textPassword" but you can only enter numbers. Take into account that if you use it the password will not be so strong, so I wouldn't recommend using it when dealing with sensitive data unless it is used along with another type of user authentication.
  4. android:inputType="textWebPassword": Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_PASSWORD and has the same behaviour as android:inputType="textPassword" but it is intended to be used in a web form i.e. inside a browser page (any web form control that takes input from a user). So this shall not be used in an EditText native control. An example of using this would be to disable AutoSuggestion from Android in a WebView by wrapping a WebView and changing EditorInfo input type to add the flag InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD inside the onCreateInputConnectionmethod.

As an example of the last one taken from the link:

public class NoSuggestionsWebView extends WebView {
    ...

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs)
    {
        InputConnection ic = super.onCreateInputConnection(outAttrs);

        outAttrs.inputType &= ~EditorInfo.TYPE_MASK_VARIATION; /* clear VARIATION type to be able to set new value */
        outAttrs.inputType |= InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD; /* WEB_PASSWORD type will prevent form suggestions */

        return ic;
    }
}

I hope it is clear now the differences and mainly between android:inputType="textPassword" and android:inputType="textWebPassword"