由于SDK封装和提供了一套基于Preference的类,使用Preference通过编辑xml配置文件,只要很少的代码就可以实现了,而且Preference本身已经实现了参数保存,不需要我们再考虑将参数保存文件.类型主要有有两类:一类是管理布局的有PreferenceScreen和PreferenceCategory;另一类是具体的设置元素,有CheckBoxPreference、ListPreference、EditTextPreference和RingtonePreference等.
具体使用:
一。1.生成一个Preference资源文件。
比如:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference android:defaultValue="true" android:summaryOff="@string/auto_search_disable" android:summaryOn="@string/auto_search_enabled" android:key="@string/auto_search_enable_key" android:title="@string/auto_search_enable_title" android:disableDependentsState="false"> </CheckBoxPreference> <ListPreference android:dialogTitle="@string/search_rang_key" android:entryValues="@array/search_rang_values" android:entries="@array/search_rang_strings" android:positiveButtonText="@string/enterOk" android:negativeButtonText="@string/cancle" android:dependency="@string/auto_search_enable_key" android:title="@string/search_rang_key" android:summary="@string/search_rang_summery" android:key="@string/search_rang_key" android:defaultValue="@string/search_rang_default_value"> </ListPreference> </PreferenceScreen>
2.继承之PreferenceActivity代码。
public class ListPreferenceActi extends PreferenceActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
3.加载布局。
addPreferencesFromResource(R.xml.listpreference);
效果如下:
二。使用PreferenceCategory
1.布局:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="Settings"> <PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" android:title="Emotions" android:summary="settings about emotions"> <CheckBoxPreference android:title="Love me?" android:summaryOn="Yes,I love you!" android:summaryOff="No,I am sorry." android:defaultValue="true" android:key="@string/category_loveme_key"> </CheckBoxPreference> <CheckBoxPreference android:title="Hate me?" android:summaryOn="Yes,I hate you!" android:summaryOff="No,you are a good person." android:defaultValue="false"> </CheckBoxPreference> </PreferenceCategory> <PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" android:title="Relations" android:summary="settings about relations"> <CheckBoxPreference android:title="Family?" android:summaryOn="Yes,we are family!" android:summaryOff="No,I am sorry." android:defaultValue="true"> </CheckBoxPreference> <CheckBoxPreference android:title="Friends?" android:summaryOn="Yes,we are friends!" android:summaryOff="No,I am sorry." android:defaultValue="false"> </CheckBoxPreference> </PreferenceCategory> </PreferenceScreen>
2.调用:
addPreferencesFromResource(R.xml.preferencescategory);
3.效果:
三。使用PreferenceScreen
1.布局:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="Settings"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="Emotions" android:summary="settings about emotions"> <CheckBoxPreference android:title="Love me?" android:summaryOn="Yes,I love you!" android:summaryOff="No,I am sorry." android:defaultValue="true" android:key="@string/screen_loveme_key" android:disableDependentsState="false"> </CheckBoxPreference> <CheckBoxPreference android:title="Hate me?" android:summaryOn="Yes,I hate you!" android:summaryOff="No,you are a good person." android:defaultValue="false"> </CheckBoxPreference> </PreferenceScreen> <PreferenceScreen android:title="Relations" android:summary="settings about relations" xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference android:title="Family?" android:summaryOn="Yes,we are family!" android:summaryOff="No,I am sorry." android:defaultValue="true"> </CheckBoxPreference> <CheckBoxPreference android:title="Friends?" android:summaryOn="Yes,we are friends!" android:summaryOff="No,I am sorry." android:defaultValue="false"> </CheckBoxPreference> </PreferenceScreen> </PreferenceScreen>
2.调用:
addPreferencesFromResource(R.xml.preferencesscreen);
3.效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?