我有以下代码
<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"
,编辑文本字段也不会被点屏蔽。
我用的是雄激素。谁来帮帮忙
更新
我试着按照一位评论者的建议跟随,但没有运气。
<EditTextPreference
android:key="pref_password"
android:title="Password"
app:iconSpaceReserved="false"
android:dialogTitle="Password"
android:inputType="textPassword"/>
发布于 2019-07-15 01:17:21
直接在EditTextPreference上设置属性不适用于AndroidX库--因为EditTextPreference不是‘an 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
}
https://stackoverflow.com/questions/57018865
复制相似问题