3

我从 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 刷新其值?

4

2 回答 2

1

解决方案在ApiDemos AdvancedPreferences.java中:

private CheckBoxPreference mCheckBoxPreference;

mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
        KEY_ADVANCED_CHECKBOX_PREFERENCE);

if (mCheckBoxPreference != null) {
    mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked());
}
于 2011-04-17T16:47:57.283 回答
0

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

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

注意!

于 2011-04-13T09:08:27.917 回答