在设置屏幕上隐藏密码

Dar*_*ius 5 android android-preferences

我有一个简单的问题,但找不到解决方案。让我们在Android Studio上生成设置页面。这是一个字段-密码。

<EditTextPreference
    android:defaultValue="@string/pref_default_display_password"
    android:inputType="textPassword"
    android:key="password"
    android:maxLines="1"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:password="true"
    android:title="@string/pref_title_display_password" />
Run Code Online (Sandbox Code Playgroud)

带有星号的输入没有问题。但是问题是,我在屏幕上看到保存的密码:

在此处输入图片说明

我怎么藏起来?

非常感谢你。

ale*_*xtk 6

我使用OnPreferenceChangeListener解决了该问题,该问题在显示首选项和更改首选项时调用。我借此机会在摘要中设置密码的修改版本,方法是将文本转换为带星号的字符串

Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();
        ...
        if (preference instanceof EditTextPreference){
            // For the pin code, set a *** value in the summary to hide it
            if (preference.getContext().getString(R.string.pref_pin_code_key).equals(preference.getKey())) {
                stringValue = toStars(stringValue);
            }
            preference.setSummary(stringValue);
        }
        ...
        return true;
    }
};

String toStars(String text) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < text.length(); i++) {
        sb.append('*');
    }
    text = sb.toString();
    return text;

}
Run Code Online (Sandbox Code Playgroud)