gpt4 book ai didi

Android:onSharedPreferenceChanged 不改变 PreferenceScreen 的摘要

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:48 27 4
gpt4 key购买 nike

我有一个子屏幕:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Hra">
<PreferenceScreen
android:key="pref_game_plus_category"
android:title="@string/operation_plus"
android:persistent="false">

<CheckBoxPreference
android:key="pref_game_operation_plus"
android:title="@string/pref_title_operation_plus"
android:defaultValue="true"
/>

我在中实例化首选项

public class GamePreferenceActivity extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
log.debug("onSharedPreferenceChanged(" + key + ")");
PreferenceScreen preferenceScreen = getPreferenceScreen();
...
case "pref_game_operation_plus":
preferenceScreenHelper.setScreenSummary("plus", preferenceScreen, sharedPreferences);

这里我检测复选框的状态并设置父屏幕字幕:

public void setScreenSummary(String key, PreferenceScreen preferenceScreen, SharedPreferences sharedPreferences) {
boolean value = sharedPreferences.getBoolean("pref_game_operation_" + key, true);
Preference preference = preferenceScreen.findPreference("pref_game_" + key + "_category");
preference.setSummary(value ? R.string.pref_operation_enabled : R.string.pref_operation_disabled);

当我调试这段代码时,"pref_game_plus_category" 已经更新了它的摘要,但是当我返回根首选项时,摘要并没有改变。

PS 当我重新启动首选项 Activity 时,摘要会反射(reflect)当前值。它初始化为:

public void setScreenSummary(String key, Preference preference, SharedPreferences sharedPreferences) {
boolean value = sharedPreferences.getBoolean("pref_game_operation_" + key, true);
preference.setSummary(value ? R.string.pref_operation_enabled : R.string.pref_operation_disabled);
}

更新:摘要是第一次更新,但后续更改(即使是不同的屏幕)都没有效果。奇怪的是,我可以两次看到此首选项的条目日志,但调试器只停止一次。我必须深入研究 Android 资源。

04-23 08:36:46.471 5503-5503/lelisoft.com.lelimath D/l.c.l.a.GamePreferenceActivity: onSharedPreferenceChanged(pref_game_divide_category)
04-23 08:36:48.004 5503-5503/lelisoft.com.lelimath D/l.c.l.a.GamePreferenceActivity: onSharedPreferenceChanged(pref_game_operation_divide)
04-23 08:36:48.006 5503-5503/lelisoft.com.lelimath D/l.c.l.h.PreferenceHelper: pokus
04-23 08:36:48.007 5503-5503/lelisoft.com.lelimath D/l.c.l.a.GamePreferenceActivity: onSharedPreferenceChanged(pref_game_operation_divide)

最佳答案

将您的 setScreenSummary 函数更改为以下内容:

public void setScreenSummary(String key, PreferenceScreen preferenceScreen, SharedPreferences sharedPreferences) {
boolean value = sharedPreferences.getBoolean("pref_game_operation_" + key, true);
//instead of getting a reference to preference typecast it to PreferenceScreen
PreferenceScreen preference = (PreferenceScreen) findPreference("pref_game_" + key + "_category");
//now set the summary
preference.setSummary(value ? R.string.pref_operation_enabled : R.string.pref_operation_disabled);

//here comes the important part
//get the listView adapter and call notifyDataSetChanged
//This is necessary to reflect change after coming back from sub-pref screen
((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();

}

我在 gitlab 上有一个演示项目 PreferenceScreenDemo .您可以检查整个代码。或者可以直接跳到this part of the demo app

关于Android:onSharedPreferenceChanged 不改变 PreferenceScreen 的摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36807299/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com