问题
I am trying to use the mColorPicker and have it running good, it changes the color in the picker but I am trying to get the shared preferences to get the new color so I can use it. So far no luck with this, it doesn't change the color but keeps the default color only. I have the preferences in a separate file and in the main activity is where I want to get the preferences from. Heres what I have in the main activity
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
this._blimp.setTopNameColor(prefs.getInt(TOPNAME_COLOR_CHOICE, 0xffffffff));
}
when I use the log for this I get a -1 for this, 0xffffffff is the default color but it doesn't change here at all. So I know I am missing something here, in the settings I have this
@Override
public boolean onPreferenceClick(Preference preference) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(LiveWallpaperSettings.this);
final ColorPickerDialog d = new ColorPickerDialog(this, prefs.getInt("top_name_color", 0xffffffff));
d.setAlphaSliderVisible(true);
d.setButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("top_name_color", d.getColor());
editor.commit();
}
});
d.setButton2("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
d.show();
return true;
}
Which I took from the Main.java in the mColorPicker and put it in my livewallpapersettings file instead. In here it changes the dialog window color no problem but not in the shared preference in my main activity file. This is a fantastic color picker if I can just get it to work and update in the shared preferences in the main activity so it actually changes my color. I know it's probably something simple but hopefully someone can help me out here. Thanks in advance. Sam
回答1:
I have used this ColorPicker and it works great.
Have a button in your preferences to show the dialog when pressed.
I use
SharedPreferences sharedPreferences = getSharedPreferences(MY_PREFERENCES, Activity.MODE_PRIVATE);
editor = sharedPreferences.edit();
and then when the user choose the color
editor.putInt(TEXT_COLOR, color);
editor.commit();
In your main activity you will get the color by
prefs = getSharedPreferences(Preferences.MY_PREFERENCES, Activity.MODE_PRIVATE);
textColor = prefs.getInt(Preferences.TEXT_COLOR, R.color.black);
Hope this helps.
来源:https://stackoverflow.com/questions/9368725/android-shared-preferences-mcolorpicker-issue