开发者

onSharedPreferenceChanged edit stored value?

If I have a onSharedPreferenceChanged event in my PreferenceActivity, that is checking if a CheckBoxPreference is checked or not and setting setEnabled on some other Preference, does the actual value of the other Preference get changed in the SharedPreferences, or do I have to manually set them?

What I have is:

 public void onSharedPreferenceChanged(Settings sharedPreferences, String key) 
 {
      CheckBoxPreference cbUpdatesEnabled = (CheckBoxPreference)getPreferenceScreen().findPreference("updatesenabled");

      CheckBoxPreference cbVibrate = (CheckBoxPreference)getPreferenceScreen().findPreference("vibrate");

      cbVibrate.setEnabled(cbUpdatesEnabled.isChecked());
 }

Which toggles whether the "vibrate" checkbox is enabled or disabled in the UI, but then do I also have to use:

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferen开发者_Python百科ces.edit();
editor.putBoolean("vibrate", cbUpdatesEnabled.isChecked());
editor.commit();

Or is there some other way of updating the SharedPreferences? It doesn't look like the value is automatically saved just based on if it's enabled or not.


You don't have to manually update the preference value if you are using PreferenceActivity. For more information check following tutorials.

Click here

Click here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜