当存储的Andr​​oid preference键? [英] Where to store Android preference keys?

查看:120
本文介绍了当存储的Andr​​oid preference键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建preference活动我定义所有的preferences在XML文件中。每preference在这个XML定义的键。但是,当我访问preference我写的:

When I create preference activity I define all preferences in xml file. Every preference has a key defined in this xml. But when I access preference I write:

SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean foo_value = appPreferences.getBoolean("foo_key_defined_in_xml", false);

有什么办法避免引用foo_key_defined_in_xml在硬codeD的方式? 也许有可能引用它在研发样式方式(未指字符串)?

Is there any way to avoid referencing "foo_key_defined_in_xml" in hard-coded way? Maybe there is a possibility to reference it in R style way (not to refer to string)?

推荐答案

我发现它可以存储密钥在的strings.xml ,并把它们从 preferences的.xml 就像所有其他值机器人:键=@字符串/ preference_key

I've found that it's possible to store keys in strings.xml and refer to them from preferences.xml just like all other values android:key="@string/preference_key".

在code可以通过键入参考键的getString(R.string。preference_key)

In code you can refer to key by typing getString(R.string.preference_key)

这篇关于当存储的Andr​​oid preference键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆