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>

設定(チェックボックス)の1つは、他のチェックボックスの状態を変更します。

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

解決策はApiDemosAdvancedPreferences.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 に答える