语雀原文链接
1 作用
PreferenceActivity是Android中专门用来实现程序设置界面及参数存储的一个Activity。从Android 3.0以后官方不再推荐直接让PreferenceActivity加载选项设置布局文件,而是建议使用PreferenceFragment(此类在API级别28中已弃用。替代类androidx库下的PreferenceFragmentCompat,有关androidx的问题有很多,这里不过多介绍 ),二者用法类似。PreferenceActivity在API级别29中已被弃用。PreferenceActivity与普通的Activity不同,它不是使用界面布局文件,而是使用选项设置的布局文件。
2 基本用法
第一步:PreferenceActivity
public class MyPreferenceActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 在这里,R.xml.mylistperference可以看作是布局文件
addPreferencesFromResource(R.xml.activity_my_preference);
}
}
第二步:清单文件声明
<activity
android:name=".MyPreferenceActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
第三步:编辑布局
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- android:key 唯一标识符,和android:id相类似,PreferenceManager可以以其为参数通过findPreference获取指定的preference-->
<!-- 注意,这个android:key的值也是Preference文件里面的XML“键”名-->
<!-- android:title 大标题-->
<!-- android:summary 标题下面的小字(这个要作为选项卡才有)-->
<!-- android:entries 弹出的对话框中,列表显示的文本内容,注意哦,这里指定的是一个数组哦-->
<!-- android:entryValues 与android:entries相对应的值-->
<!-- android:defaultValue 当对应值不存在时的默认值-->
<!-- android:dialogTitle 弹出的对话框中的标题信息-->
<ListPreference
android:key="myListPreference"
android:title="标题"
android:summary="说明摘要"
android:entries="@array/list_entry"
android:entryValues="@array/list_entry_value"
android:dialogTitle="这里是弹出框"
android:defaultValue="@string/deflault_list_entries_value"/>
</PreferenceScreen>
对应的资源string
<string-array name="list_entry_value">
<item>shandong</item>
<item>fujian</item>
<item>beijing</item>
<item>hebei</item>
</string-array>
<string-array name="list_entry">
<item>山东</item>
<item>福建</item>
<item>北京</item>
<item>河北</item>
</string-array>
<string name="deflault_list_entries_value">beijing</string>
第四步:运行效果
第五步:数据是保存在data/data/包名/shared_prefs/包名_preference.xml中
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="myListPreference">hebei</string>
</map>
3 常用选项控件
修改布局
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:defaultValue="@string/deflault_list_entries_value"
android:dialogTitle="这里是弹出框"
android:entries="@array/list_entry"
android:entryValues="@array/list_entry_value"
android:key="myListPreference"
android:summary="说明摘要"
android:title="标题" />
<CheckBoxPreference
android:key="medf"
android:summaryOff="马尔代夫未被选中"
android:summaryOn="马尔代夫被选中了"
android:title="马尔代夫" />
<CheckBoxPreference
android:key="mlqs"
android:summaryOff="毛里求斯未被选中"
android:summaryOn="毛里求斯被选中了"
android:title="毛里求斯" />
<EditTextPreference
android:dialogTitle="输入您的名称:"
android:key="editWord"
android:defaultValue="im human"
android:summary="简要说明"
android:title="输入名称" >
</EditTextPreference>
</PreferenceScreen>
CheckBoxPreference效果
EditTextPreference效果
最终的数据保存在data/data/包名/shared_prefs/包名_preference.xml中
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="mlqs" value="false" />
<string name="myListPreference">hebei</string>
<boolean name="medf" value="true" />
<string name="editWord">qwwe</string>
</map>
4 PreferenceScreen分组
目的很明显的,就是为了更美观。把不同类型的选项进行归类,提高用户体验。
就是制造二(多)级页面。只有点进去才会看到详细选项
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 第一组 -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="edittext_screen"
android:summary="点击进入第一组首选项"
android:title="第一组" >
<EditTextPreference
android:dialogTitle="输入您的名称:"
android:key="editTitlePreference1"
android:summary="简要说明"
android:title="输入名称" >
</EditTextPreference>
</PreferenceScreen>
<!-- 第二组 -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="edittext_screen"
android:summary="点击进入第二组首选项"
android:title="第二组" >
<EditTextPreference
android:dialogTitle="输入您的名称:"
android:key="editTitlePreference2"
android:summary="简要说明"
android:title="输入名称" >
</EditTextPreference>
</PreferenceScreen>
<!-- 第三组 -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="edittext_screen"
android:summary="点击进入第三组首选项"
android:title="第三组" >
<EditTextPreference
android:dialogTitle="输入您的名称:"
android:key="editTitlePreference3"
android:summary="简要说明"
android:title="输入名称" >
</EditTextPreference>
</PreferenceScreen>
</PreferenceScreen>
5 PreferenceCategory分组
如果不想打开二级页面中打开,可以使用PreferenceCategory替换之前的PreferenceScreen,这样所有的设置都在一个页面进行分组
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 第一组 -->
<PreferenceCategory
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="edittext_screen"
android:summary="点击进入第一组首选项"
android:title="第一组" >
<EditTextPreference
android:dialogTitle="输入您的名称:"
android:key="editTitlePreference1"
android:summary="简要说明"
android:title="输入名称" >
</EditTextPreference>
<EditTextPreference
android:dialogTitle="输入您的名称:"
android:key="editTitlePreference11"
android:summary="简要说明"
android:title="输入名称" >
</EditTextPreference>
</PreferenceCategory>
<!-- 第二组 -->
<PreferenceCategory
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="edittext_screen"
android:summary="点击进入第二组首选项"
android:title="第二组" >
<EditTextPreference
android:dialogTitle="输入您的名称:"
android:key="editTitlePreference2"
android:summary="简要说明"
android:title="输入名称" >
</EditTextPreference>
</PreferenceCategory>
<!-- 第三组 -->
<PreferenceCategory
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="edittext_screen"
android:summary="点击进入第三组首选项"
android:title="第三组" >
<EditTextPreference
android:dialogTitle="输入您的名称:"
android:key="editTitlePreference3"
android:summary="简要说明"
android:title="输入名称" >
</EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>