我有以下代码
<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/