Android 应用程序中每个视图的单独首选项
当应用程序运行时,我有多个视图来来去去。我希望每个视图都有自己的个人偏好,这些偏好存储为视图的 ID 标签。在这些之上是“常规首选项”,子首选项在创建视图时引用它来获取其默认值。
现在我已将常规首选项设置为默认的共享首选项。但我不知道如何创建新的首选项并设置活动 UI 以便用户可以更改它们。它与设置 SharedPreferences 几乎相同吗?
I have multiple views that come and go as the application runs. I want each view to have its own personal preferences that are stored as the ID tag of the view. Above these is the "General Preferences" that the sub prefs reference to get their default values when a view it is created.
Right now I have it set up that the General Preferences are the default SharedPreferences. But I have no Idea how to create the new preferences and set up an activity UI so the user can change them. Is it pretty much the same as setting up the SharedPreferences?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能不完全是您所要求的,但这就是我所做的:
在我的主要活动中,当我调用首选项活动时,我将自定义首选项文件的名称作为意图中的额外数据传递给它:
然后,在我的首选项活动中,我获取自定义首选项名称并将其设置如下:
最后,在我的主要活动中,我获得如下所示的特定首选项:
this may not be exactly what you're asking for, but here's what I do:
in my main activity, when I call the preferences activity, I pass it the name of the custom preference file as extra data in the intent:
then, in my preferences activity, I get the custom preferences name and set it like this:
lastly, in my main activity, I get specific preferences like this:
不知怎的,我不值得评论,但值得写一个答案,所以我们开始吧:
我真的很想知道如何将sharedPreferences与PreferencesActivity一起使用而不是DefaultSharedPreferences。
我能想到的实现此目的的一种方法是让preferenceActivity 将值保存到defaultSharedPreferences,然后读取这些值并将它们保存到与所保存值的类型相匹配的名称关联的sharedPreferences 中。
但这似乎是非常错误的。那么你们是怎么做到的呢?或者您是否将任何 PreferencesActivties 中的所有值保存到 defaultSharedPreferences 中?
Somehow I am not worthy to comment but to write an answer, so here we go:
I'd really like to know how to use sharedPreferences with PreferencesActivity instead of DefaultSharedPreferences.
One way I can think of to accomplish this is letting the preferenceActivity save the values to defaultSharedPreferences and then read these values out and save them into a sharedPreferences associated with a name that would match the kind of values saved.
But this seems very wrong. So how do you guys do this? Or do you save all your values from any PreferencesActivties into defaultSharedPreferences?
您可以使用 PreferenceManager 来实现该目标。
You can use PreferenceManager to achieve the objective.