簡體   English   中英

Android:onSharedPreferenceChanged不會更改PreferenceScreen的摘要

[英]Android: onSharedPreferenceChanged does not change a summary of PreferenceScreen

我有一個子屏幕:

<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當我重新啟動首選項活動時,摘要反映當前值。 它初始化為:

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上有一個演示項目。 您可以檢查整個代碼。 或者您可以直接跳到演示應用程序的這一部分

我注意到你正在監聽OnSharedPreferenceChangeListener ,嘗試監聽/實現OnPreferenceChangeListener - 然后處理

    onPreferenceChange(Preference preference, Object newValue){
    ...
    if(preference.getKey().equals("pref_game_operation_plus")){
       boolean enabled = Boolean.parseBoolean(newValue);
       preference.setSummary(enabled);
    }
   ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM