Android系统默认值preference [英] Default value of Android preference
本文介绍了Android系统默认值preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你如何在XML中定义一个Android preference的默认值?
How do you get the default value of an Android preference defined in XML?
上下文:我不想在这两个code和preferences XML重复的默认值的定义
Context: I don't want to repeat the definition of the default value in both the code and the preferences XML.
推荐答案
您可以定义资源的默认值:
You can define default value in resources:
<resources>
<bool name="mypreference_default">true</bool>
</resources>
使用中的值 preferences.xml
:
<CheckBoxPreference
android:defaultValue="@bool/mypreference_default"
android:key="mypreference"
android:title="@string/mypreference_title" />
然后在code使用:
Then use in code:
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
Boolean value = context.getResources().getBoolean(R.bool.mypreference_default);
Boolean b = p.getBoolean("mypreference", value);
这篇关于Android系统默认值preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文