Android之首选项相关 Preferences(四)组织首选项
2015 年 8 月 30 日
何谓组织首选项啊,实际上就是为首选项分组!
分组之后,我们首页只显示组名,当我们点击进去的时候,才会显示具体的首选项列表。如图:
代码:
- xml version=“1.0” encoding=“utf-8”?>
- <PreferenceScreen
- xmlns:Android=“http://schemas.android.com/apk/res/android”
- android:key=“edittext_screen”
- android:title=“屏幕标题”
- android:summary=“屏幕简要说明”
- >
- <PreferenceScreen
- xmlns:android=“http://schemas.android.com/apk/res/android”
- android:key=“edittext_screen”
- android:title=“第一组”
- android:summary=“点击进入第一组首选项”
- >
- <RingtonePreference
- android:key=“ringtonePreference”
- android:summary=“简要说明”
- android:title=“选择系统铃声”
- android:ringtoneType=“alarm”
- android:showSilent=“true”
- >
- RingtonePreference>
- PreferenceScreen>
- <PreferenceScreen
- xmlns:android=“http://schemas.android.com/apk/res/android”
- android:key=“edittext_screen”
- android:title=“第二组”
- android:summary=“点击进入第二组首选项”
- >
- <EditTextPreference
- android:dialogTitle=“输入您的名称:”
- android:key=“editTitlePreference”
- android:summary=“简要说明”
- android:title=“输入名称”
- >EditTextPreference>
- PreferenceScreen>
- <PreferenceScreen
- xmlns:android=“http://schemas.android.com/apk/res/android”
- android:key=“edittext_screen”
- android:title=“第三组”
- android:summary=“点击进入第三组首选项”
- >
- <EditTextPreference
- android:dialogTitle=“输入您的名称:”
- android:key=“editTitlePreference”
- android:summary=“简要说明”
- android:title=“输入名称”
- >EditTextPreference>
- PreferenceScreen>
- PreferenceScreen>
以上的这种方法适合首选项的数目较多时使用。
如果我们首选项的数目较少,但是我们依旧想为他们分组下,怎么办呢?
我们可以将上面代码中的嵌套PreferenceScreen改为PreferenceCategory,就这么简单!!!
- xml version=“1.0” encoding=“utf-8”?>
- <PreferenceScreen
- xmlns:android=“http://schemas.android.com/apk/res/android”
- android:key=“edittext_screen”
- android:title=“屏幕标题”
- android:summary=“屏幕简要说明”
- >
- <PreferenceCategory
- xmlns:android=“http://schemas.android.com/apk/res/android”
- android:key=“edittext_screen”
- android:title=“第一组”
- android:summary=“点击进入第一组首选项”
- >
- <RingtonePreference
- android:key=“ringtonePreference”
- android:summary=“简要说明”
- android:title=“选择系统铃声”
- android:ringtoneType=“alarm”
- android:showSilent=“true”
- >
- RingtonePreference>
- PreferenceCategory>
- <PreferenceCategory
- xmlns:android=“http://schemas.android.com/apk/res/android”
- android:key=“edittext_screen”
- android:title=“第二组”
- android:summary=“点击进入第二组首选项”
- >
- <EditTextPreference
- android:dialogTitle=“输入您的名称:”
- android:key=“editTitlePreference”
- android:summary=“简要说明”
- android:title=“输入名称”
- >EditTextPreference>
- PreferenceCategory>
- <PreferenceCategory
- xmlns:android=“http://schemas.android.com/apk/res/android”
- android:key=“edittext_screen”
- android:title=“第三组”
- android:summary=“点击进入第三组首选项”
- >
- <EditTextPreference
- android:dialogTitle=“输入您的名称:”
- android:key=“editTitlePreference”
- android:summary=“简要说明”
- android:title=“输入名称”
- >EditTextPreference>
- PreferenceCategory>
- PreferenceScreen>
效果图可就大变样了