首选项和复选框首选项
PreferenceActivity 的 文档 指出“这些首选项将自动保存到 SharedPreferences当用户与它们交互时。”在这种情况下,如果我子类化 PreferenceActivity,在 onCreate 中调用 addPreferencesFromResource,在 UI 中设置一些首选项,我希望对我的首选项所做的任何更改都应该自动保存;当我稍后返回到我的首选项屏幕时,所有首选项都应该恢复。
我问的原因是 CheckBoxPreference 的行为不符合预期,当然假设我的理解是正确的。我希望如果我有一个 CheckBoxPreference,用户的选择应该在更改时自动保存。难道不是这样吗?
那么有人可以确认我的理解或在必要时纠正我吗?
我可以创建一个自定义首选项屏幕,在其中处理代码中的所有更改,但我更喜欢使用推荐的方法。我只是想知道是否有错误。
我只想说我现在有点沮丧。
顺便说一句,我正在针对 api v2.0 进行开发。
The documentation for the PreferenceActivity states "These preferences will automatically save to SharedPreferences as the user interacts with them." That being the case, if I subclass PreferenceActivity, invoke addPreferencesFromResource in onCreate, set some preferences in the UI I would expect that any changes made to my preferences should automagically be saved; and when I return to my preference screen later all preferences should be restored.
The reason I'm asking is CheckBoxPreference is not behaving as expected, assuming my understanding is correct of course. I would expect if I have a CheckBoxPreference the users selection should be saved automatically upon change. Is this not the case?
So can anyone please confirm my understanding or correct me if necessary?
I can create a custom preference screen where I handle all the changes in code but I'd prefer to use the recommended approach. I 'm just wondering if there is a bug.
Suffice it to say I am a wee bit frustrated at the moment.
BTW I'm developing against v2.0 of the api.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果他们做出积极的选择,这效果很好。如果他们访问您的 Activity 并且没有触摸
CheckboxPreference
,它将保持默认状态。This works fine, if they make a positive selection. If they visit your activity and do not touch a
CheckboxPreference
, it will remain at the default.事实证明我的错误很简单。
我使用 strings.xml 来存储复选框键的名称,并在运行时取消引用它,就像我们对标签等所做的那样。好吧,这个解析的值中有一个空格,所以它当然不起作用。
错误的字符串条目是这样的:
它应该是这样的:
DOH!!!!
It turns out my error was quite simple.
I'm using strings.xml to store the name of the checkbox key and dereferencing that at run time like we do with labels etc. Well the value this was resolving to had a space in it so of course it wouldn't work.
The errant string entry was something like this:
And it should have been something like this:
DOH!!!!