【发布时间】:2020-11-24 01:59:35
【问题描述】:
在 Android 程序中启用/禁用和显示/隐藏首选项似乎应该非常简单。但除了直接在 XML 文件本身中之外,我无法让它工作。我只能找到不推荐使用的首选项的教程和示例。这是一个示例,我正在尝试隐藏和禁用以下两个首选项。
root_preferences.xml
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="hidePref"
app:title="Hide/Show"
app:summary="Change this preference's visibility"/>
<Preference
android:key="disablePref"
app:title="Enable/Disable"
app:summary="Enable or Disable this preference"/>
</androidx.preference.PreferenceScreen>
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
...
val sharedPreferences = Preferencemanager.getDefaultSharedPreferences(this)
//No idea how to do the following but here are guess that obviously do not work
sharedPreferences.edit().isVisible("hidePref", View.GONE).apply()
sharedPreferences.edit().isEnabled("disabledPref", false).apply()
...
}
【问题讨论】:
标签: android kotlin sharedpreferences android-preferences