EditTextPreference即使设置了android:inputType="textPassword"也不能遮蔽密码。

8
我有以下代码。
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <EditTextPreference
            app:key="pref_password"
            app:title="Password"
            app:iconSpaceReserved="false"
            app:dialogTitle="Password"
            android:inputType="textPassword"/>

</androidx.preference.PreferenceScreen>

但即使使用android:inputType="textPassword",编辑文本框也没有用点掩盖

我正在使用androidx,请有人帮忙

更新

如评论者建议的那样尝试了以下方法,但没有成功

<EditTextPreference
            android:key="pref_password"
            android:title="Password"
            app:iconSpaceReserved="false"
            android:dialogTitle="Password"
            android:inputType="textPassword"/>

请使用 "android:" 而不是 "app:"。链接:https://dev59.com/Q2025IYBdhLWcg3wLizo - Viraj S
@VirajS 尝试了,但没有成功。已更新问题。 - user158
很抱歉一直回滚!回滚按钮出现了奇怪的问题。(我本意是将问题回滚到添加 [tag:kotlin] 标签之前,因为这个标签与问题无关) - Edric
1
@Edric,实际上我正在使用 Kotlin。 - user158
仅仅因为你使用Kotlin并不意味着你可以添加 [tag:kotlin] 标签。只有当标签是问题本身的主题时,才应该添加标签:https://stackoverflow.com/help/tagging - Edric
1个回答

12

在AndroidX库中,直接在EditTextPreference上设置属性是无效的——因为EditTextPreference不是一个EditText,并且不应该作为EditText来使用。相反,您应该使用OnBindEditTextListener在显示EditText时自定义它。 (需要androidx.preference:preference v1.1.0及更高版本)

有关更多信息,请参见设置指南

使用以下代码进行编辑:

Java:

EditTextPreference preference = findPreference("pref_password");

if (preference!= null) {
    preference.setOnBindEditTextListener(
            new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            });
}

Kotlin:

val editTextPreference: EditTextPreference? = findPreference("pref_password")

        editTextPreference?.setOnBindEditTextListener {editText ->  
            editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }

你可以添加代码片段来掩盖文本,这样答案看起来更完整。 - user158

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接