android - 即使使用 android :inputType ="textPassword",EditTextPreference 也不会屏蔽密码

标签 android android-preferences android-jetpack

我有以下代码

<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"/>

最佳答案

直接在 EditTextPreference 上设置属性不适用于 AndroidX 库 - 因为 EditTextPreference 不是 EditText,也不应该这样工作。相反,您应该使用 OnBindEditTextListener 在显示 EditText 时对其进行自定义。 (需要 androidx.preference:preference v1.1.0 及更高版本)

请参阅settings guide了解更多信息

使用代码编辑:

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
        }

关于android - 即使使用 android :inputType ="textPassword",EditTextPreference 也不会屏蔽密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57018865/

相关文章:

android - 摇动手势不适用于具有 native react 的物理设备

android - 将 ArrayList 保存在共享首选项中

android - 使用 PreferenceFragment 构建的设置页面中的工具栏

android - 使用 fragment 和导航组件泄漏 nestedscrollView

android - 设置 ImageView 的权重和参数

android - 如何在点击底部导航时禁用从中心到边缘的灰色动画

android - Paging 3 - 如何处理自 beta01 以来出现的旧 APPEND RemoteMediator 请求?

android - Android Jetpack Compose Beta 是否已分离 onCommit{} 的构建器?

android - 如何获得网络响应,即使状态代码不是 200

android - 来自 PreferenceActivity 的 ClassCastException