Android sets whether the password text temporarily displays characters

Android sets whether the password text temporarily displays characters

This article refers to the control display of the string whose Edittext is password type.
For example, the password string can be displayed for a short time by default, but it cannot be displayed after being pressed, which is controllable.

1. Effect

There is a switch control in the system Setting Privacy, as shown in the following figure:
insert image description here

The specific effects are as follows:
the default (password string is displayed temporarily) effect and the setting does not display the password type string effect, as follows:

insert image description here

Two, realize

1. Whether setting and obtaining passwords are visible

The implementation is actually very simple, just the following code:

//设置密码是否短暂可见
public void setIsShowPasswordChecked(boolean isChecked) {
    Settings.System.putInt(mContext.getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,isChecked ? 1 : 0);
}

//获取密码是否短暂可见
public boolean isShowPasswordShortTime() {
    return 1 == Settings.System.getInt(mContext.getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD, 1);
}

Obtaining the property value does not require permission, but setting the Settings property value requires permission

<uses-permission android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />

This permission is a system permission and requires a system application to obtain it.

2. The properties of the actually modified Settings

From the Settings.java code:

/**
* Setting to showing password characters in text editors. 1 = On, 0 = Off
*/
public static final String TEXT_SHOW_PASSWORD = "show_password";

3. Whether the adb control password is temporarily visible

So adb can also control:

//获取是否设置密码短暂显示(默认/未设置前为null)
adb shell settings get system show_password

//设置密码短暂显示
adb shell settings put system show_password 1

//设置密码不短暂显示
adb shell settings put system show_password 0

As for why modifying a property can take effect immediately, it should be that the system has a place to monitor the change of the value of Settings.System.TEXT_SHOW_PASSWORD and do related operations;
but the specific code of the system is unknown! Those who like to study can take a look.

And why EditText sets the attribute android:inputType="textPassword",
it can display * instead of a string, which can also be studied. It is reasonable to deal with EditText/View, but is there any specific logic in it?

Guess you like

Origin blog.csdn.net/wenzhi20102321/article/details/127113323