PreferenceActivity 如何使用自定义首选项文件
我第一次尝试偏好设置时并不了解 PreferenceActivity
。所以现在我有一个应用程序将所有用户首选项存储在特定的首选项文件中。
我想迁移到使用 PreferenceActivity,但我也希望我的用户保留他们的偏好。
有没有办法告诉我的 PreferenceActivity 使用该特定文件来实现所有首选项?
My first attempt at preferences was without knowledge of PreferenceActivity
. So now I have an app that stores all user preferences in a specific preference file.
I want to migrate to using a PreferenceActivity but I also want my users to keep their preferences.
Is there a way to tell my PreferenceActivity to use that specific file for all preferences?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发布此内容可能为时已晚,但您可以在这里找到一个不错的解决方案
您预先设置默认共享首选项文件的名称,如下所示:
我希望这对某人有帮助。
问候。
It may be too late to post this but you can find a nice solution here
You set the name of the default shared preferences file beforehand like this:
I hope this helps somebody.
Regards.
您可以在应用程序开头读取所有首选项,然后使用将它们存储在
Preferences
中,希望有帮助
You could read all the preferences at the beginning of your app, and then store them in the
Preferences
usingI hope that helps
Maaalte 是正确的,您要做的是 onCreate 测试您的自定义文件是否存在,如果存在,请将其重命名为标准共享首选项文件名。
另一种选择是逐一读取旧的首选项,并使用共享首选项 API 在读取时添加它们,然后在完成后删除旧的首选项。
Maaalte is correct, what you want to do is onCreate test for the existence of your custom file and if it's there, rename it to standard shared preferences filename.
Another option is to read your old prefs one-by-one and use the shared preferences API to add them as you read them and then delete your old prefs when you are done.