【问题标题】:PreferenceActivity Refresh ProblemPreferenceActivity 刷新问题
【发布时间】:2011-08-04 12:54:51
【问题描述】:

我有从 xml 膨胀的 PreferemceActivity:

<PreferenceScreen
            android:title="Appearence"
            android:key="AppearencePref" >
            ......
            <PreferenceCategory
                android:title="Show Contact Photos">
                <CheckBoxPreference 
                    android:title="Show Contact Photos" 
                    android:summary="@string/show_contact_photos_preference"
                    android:defaultValue="true"
                    android:key="ShowContactPhotosCheckBoxPref_Appendix" />
            </PreferenceCategory>
       ........
</PreferenceScreen>

.......

<PreferenceScreen
            android:title="Contact Options"
            android:key="ContactOtionsPref">
            <PreferenceCategory 
                android:title="Show Contact Photos">
                <CheckBoxPreference 
                    android:title="Show Contact Photos"
                    android:defaultValue="true"
                    android:key="ShowContactPhotosCheckBoxPref" />
            </PreferenceCategory>
......            
</PreferenceScreen>

其中一个首选项(复选框)更改其他复选框的状态:

if("ShowContactPhotosCheckBoxPref_Appendix".equals(key)){
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
            boolean isChecked = prefs.getBoolean("ShowContactPhotosCheckBoxPref_Appendix", false);
            Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
            editor.putBoolean("ShowContactPhotosCheckBoxPref", isChecked);
            editor.commit();            
        }

但是当我使用 ShowContactPhotosCheckBoxPref 进入屏幕时,它仍然保留以前的首选项值... 因此,如果我单击 ShowContactPhotosCheckBoxPref_Appendix - 他的状态现在未选中,然后使用 ShowContactPhotosCheckBoxPref 进入屏幕 - 他的状态仍处于选中状态,但 SharedPreferences 中的值为 false...

如何告诉 PreferenceActivity 刷新它的值?

【问题讨论】:

    标签: android preferenceactivity


    【解决方案1】:

    解决方案在ApiDemos AdvancedPreferences.java:

    private CheckBoxPreference mCheckBoxPreference;
    
    mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
            KEY_ADVANCED_CHECKBOX_PREFERENCE);
    
    if (mCheckBoxPreference != null) {
        mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked());
    }
    

    【讨论】:

      【解决方案2】:

      您没有更改代码中的值。应该是:

      editor.putBoolean("ShowContactPhotosCheckBoxPref", !isChecked);
      

      注意!

      【讨论】:

      • 不,我更改了值。 ShowContactPhotosCheckBoxPref 的值必须与 ShowContactPhotosCheckBoxPref_Appendix 的值相同
      • 因此,如果我单击 ShowContactPhotosCheckBoxPref_Appendix - 他的状态现在未选中,然后使用 ShowContactPhotosCheckBoxPref 进入屏幕 - 他的状态仍处于选中状态,但 SharedPreferences 中的值为 false...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多