确定之前是否设置过 Android 首选项
我有一个应用程序,其中包含不同语言的用户界面文本。我不想将其保存在 strings.xml 中,因为我希望用户能够更改它们(而不依赖于手机的区域设置)。
我已经设置了阅读此内容的偏好。默认情况下,我希望 UI 采用区域设置值,但如果用户更改了 UI 语言首选项,我希望它使用该值。
有什么方法可以确定之前是否已设置首选项?问题是, getInt 让我设置默认值,因此我无法检查(例如) uiLang==null 来确定之前是否设置了首选项。
SharedPreferences settings = getPreferences(MODE_PRIVATE);
//If uiLang has not been set before, then lets use the locale.
uiLang = settings.getInt("uiLang", 1); //English (=1) es default
myUI = new UIText(uiLang);
myTextView.setText(myUI.title);
I have an app which will have the UI text in different languages. I do not want to save this in the strings.xml, because I want the user to be able to change them (and not be dependant on the phone's locale).
I've setup a preference to read this. By default, I want the UI to take the locale value, but if the user has changed the UI lang preference, I want it to use that.
Is there any way to determine if a preference has been set previously? The thing is, the getInt makes me set a default, so I can't check (for example) that uiLang==null to determine if a preference was set before or not.
SharedPreferences settings = getPreferences(MODE_PRIVATE);
//If uiLang has not been set before, then lets use the locale.
uiLang = settings.getInt("uiLang", 1); //English (=1) es default
myUI = new UIText(uiLang);
myTextView.setText(myUI.title);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用“包含”方法来检查:
http://developer.android.com/reference/android/content/SharedPreferences.html#contains%28java.lang.String%29
you could use the 'contains' method to check this:
http://developer.android.com/reference/android/content/SharedPreferences.html#contains%28java.lang.String%29