如何对小部件的多个实例使用一个首选项活动?

发布于 2024-10-10 00:43:31 字数 147 浏览 0 评论 0原文

我正在开发一个 Android 主屏幕应用程序小部件。

我正在尝试向小部件添加首选项。我可以放置多个小部件。但我想使用动态创建的共享首选项来配置它。

我如何从资源文件中加载首选项,该文件从共享首选项中获取值并更新它?

有什么建议吗?

I am working on an android home screen app widget.

i am trying to add preferences to the widget. i can put multiple widgets. but i want to configure it with preferences using shared preference created dynamically.

how can i load the preference from the resource file which fetches the value from the sharedpreference and also updates it?

any suggestion?

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

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

发布评论

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

评论(2

凌乱心跳 2024-10-17 00:43:31

问题的关键是如何使用 PreferenceActivity 保存的设置作为参考并创建自己的设置。下面是我使用单个 PreferenceActivity 实现的应用程序小部件的多个实例的多个设置。

private void onExitPreferenceActivity(Context context, int appWidgetId) {
    // Load the user selected settings saved by PreferenceActivity
    final String SETTING_PREFIX = "COLOR";
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String colorSetting = prefs.getString(SETTING_PREFIX, "");
    // Save the setting of the current widget with widget ID as the postfix
    final SharedPreferences.Editor prefEditor = prefs.edit();
    prefEditor.putString(SETTING_PREFIX + String.valueOf(appWidgetId), colorSetting);
    prefEditor.commit();
}

private void onLoadingWidgetSetting(Context context, int appWidgetId) {
    // Load the setting of a particular widget given a widget ID
    final String SETTING_PREFIX = "COLOR";
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String colorSetting = prefs.getString(SETTING_PREFIX + String.valueOf(appWidgetId), "");
    // Use the loaded setting
    // ....
}

The key to the problem is to somehow use the setting saved by PreferenceActivity as reference and create your own setting. Below is what I used to achieve having multiple setting for multiple instances of app widget with single PreferenceActivity.

private void onExitPreferenceActivity(Context context, int appWidgetId) {
    // Load the user selected settings saved by PreferenceActivity
    final String SETTING_PREFIX = "COLOR";
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String colorSetting = prefs.getString(SETTING_PREFIX, "");
    // Save the setting of the current widget with widget ID as the postfix
    final SharedPreferences.Editor prefEditor = prefs.edit();
    prefEditor.putString(SETTING_PREFIX + String.valueOf(appWidgetId), colorSetting);
    prefEditor.commit();
}

private void onLoadingWidgetSetting(Context context, int appWidgetId) {
    // Load the setting of a particular widget given a widget ID
    final String SETTING_PREFIX = "COLOR";
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String colorSetting = prefs.getString(SETTING_PREFIX + String.valueOf(appWidgetId), "");
    // Use the loaded setting
    // ....
}
香橙ぽ 2024-10-17 00:43:31

也许尝试在文件系统上的某个地方编写您自己的偏好系统。
只是建议

maybe try to write your own preference system maybe on somewhere on filesys.
just suggesting

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