Android shared preferences mColorPicker Issue

无人久伴 提交于 2019-12-25 05:38:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!