Android 共享首选项 mColorPicker 问题

发布于 2025-01-07 15:05:47 字数 1485 浏览 8 评论 0原文

我正在尝试使用 mColorPicker 并使其运行良好,它会更改选择器中的颜色,但我正在尝试获取共享首选项来获取新颜色,以便我可以使用它。 到目前为止还没有成功,它不会改变颜色,而只保留默认颜色。 我将首选项放在一个单独的文件中,并且在主要活动中我想从中获取首选项。 这是我在主要活动中的内容,

public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
     this._blimp.setTopNameColor(prefs.getInt(TOPNAME_COLOR_CHOICE, 0xffffffff));
}

当我使用日志时,我得到 -1,0xffffffff 是默认颜色,但它在这里根本没有改变。 所以我知道我在这里遗漏了一些东西,在设置中我有这个

@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;
 }

,我从 mColorPicker 中的 Main.java 中获取并将其放入我的 livewallpapersettings 文件中。在这里,它更改对话框窗口颜色没有问题,但在我的主活动文件的共享首选项中则不然。 这是一个很棒的颜色选择器,如果我能让它工作并在主要活动的共享首选项中更新,这样它实际上会改变我的颜色。 我知道这可能很简单,但希望有人可以帮助我。 提前致谢。 山姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢华的一滴泪 2025-01-14 15:05:47

我使用过这个 ColorPicker,效果非常好。

在您的首选项中有一个按钮,按下时会显示对话框。

我使用

SharedPreferences sharedPreferences = getSharedPreferences(MY_PREFERENCES, Activity.MODE_PRIVATE);
editor = sharedPreferences.edit();

,然后当用户在您的主要活动中选择颜色时,

editor.putInt(TEXT_COLOR, color);
editor.commit();

您将获得希望这有帮助的颜色

prefs = getSharedPreferences(Preferences.MY_PREFERENCES, Activity.MODE_PRIVATE);
textColor = prefs.getInt(Preferences.TEXT_COLOR, R.color.black);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文