【问题标题】:EditTextPreference does not mask password even with android:inputType="textPassword"即使使用 android:inputType="textPassword",EditTextPreference 也不会屏蔽密码
【发布时间】:2019-08-09 23:09:22
【问题描述】:

我有以下代码

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

【问题讨论】:

  • 而不是“app:”,使用“android:”。链接,stackoverflow.com/questions/6164430/…
  • @VirajS 试过了,但没有运气。更新了问题
  • 抱歉不断回滚!回滚按钮有一个奇怪的问题。 (我打算将问题回滚到添加 kotlin 标签之前,因为此标签与问题无关)
  • @Edric 实际上我使用的是 kotlin
  • 仅仅因为您使用 Kotlin 并不意味着您可以添加 kotlin 标签。仅当标签是问题本身的主题时才应添加:stackoverflow.com/help/tagging

标签: android android-preferences android-jetpack


【解决方案1】:

直接在 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);
                }
            });
}

科特林:

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

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

【讨论】:

  • 能否添加代码 sn-p 用于屏蔽文本,这样答案看起来更完整
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 2019-05-28
相关资源
最近更新 更多