我知道在OnPreferenceChangeListener
上,newValue尚未保存为true。
我希望手动保存,我该怎么办?谢谢!
fun setPreference(){
val chAutoRestore = preferenceManager.findPreference(getString(R.string.IsAutoRestore)) as CheckBoxPreference
chAutoRestore.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
logError( "Pref " + preference.key + " changed to " + newValue.toString())
//Save newValue
startOrStopScheduleRestore(mContext)
true
}
}
答案 0 :(得分:0)
它将自动存储在xml中的“共享首选项”中:
<EditTextPreference
android:defaultValue="@string/default_value"
android:key="YOUR_KEY"
android:title="@string/title"/>
要获取值,请使用:
getSharedPreferences(this).getBoolean("YOUR_KEY", false)
答案 1 :(得分:0)
此代码对我有用
val notificationPreference: SwitchPreferenceCompat? = findPreference("notifications")
notificationPreference!!.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { preference, newValue ->
// Manually save to Prefs
Utilities().putPrefBoolean(preference.key, newValue as Boolean)
// Reflect the newValue to Preference?
true
}