[TOC]
前言
自己独立开发过了比较多的app,发现关于设置的UI和相关的处理自己写不旦增加类开发工作量,而且常常会使整个代码层次和逻辑会变的相当混乱,让人头痛,在不考虑自己花时间弄一个设置管理类管理好设置代码,及自己处理各种UI控件的前提下,使用官方提供的Preference是最优也是最高效的选择;
Android Preference 提供的控件
这里只列举常用的support.v7,其他的也大同小异
引入方式,gradle 下配置
com.android.support:preference-v7:27.1.1
官方总结-科学上网, 使用的时候查询对应属性设置
↳ android.support.v7.preference.Preference
↳ Known Direct Subclasses
DialogPreference,PreferenceGroup,SeekBarPreference,TwoStatePreference
Known Indirect Subclasses
CheckBoxPreference,DropDownPreference,EditTextPreference,ListPreference, MultiSelectListPreference,PreferenceCategory,PreferenceScreen, SwitchPreference, SwitchPreferenceCompat
Xml案例(For example:):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pref_sync"
android:title="@string/pref_sync"
android:summary="@string/pref_sync_summ"
android:defaultValue="true" />
<ListPreference
android:dependency="pref_sync"
android:key="pref_syncConnectionType"
android:title="@string/pref_syncConnectionType"
android:dialogTitle="@string/pref_syncConnectionType"
android:entries="@array/pref_syncConnectionTypes_entries"
android:entryValues="@array/pref_syncConnectionTypes_values"
android:defaultValue="@string/pref_syncConnectionTypes_default" />
</PreferenceScreen>
Java 案例:
public class SettingsActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
不同版本大致区别
- v14 Preference Support Library
com.android.support:preference-v14:27.1.1
增加了 PreferenceFragment.OnPreferenceStartFragmentCallback 和PreferenceFragment.OnPreferenceStartScreenCallback, 更新了一些类, 如:`MultiSelectListPreference and PreferenceFragment - v17 Preference 主要是增加了对TV的支持;
具体流程
注意: findPreference/onPreferenceTreeClick 等方法在android.preference.PreferenceActivity Api16 (4.1) 中被弃用,在PreferenceFragmentCompat中能正常使用,<font color=red face=STHupo>故官方推荐使用PreferenceFragmentCompat;</font>
新建xml
使用组(Group)
案例:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_sms_storage_title"
android:key="pref_key_storage_settings">
<CheckBoxPreference
android:key="pref_key_auto_delete"
android:summary="@string/pref_summary_auto_delete"
android:title="@string/pref_title_auto_delete"
android:defaultValue="false"... />
<Preference
android:key="pref_key_sms_delete_limit"
android:dependency="pref_key_auto_delete"
android:summary="@string/pref_summary_delete_limit"
android:title="@string/pref_title_sms_delete"... />
<Preference
android:key="pref_key_mms_delete_limit"
android:dependency="pref_key_auto_delete"
android:summary="@string/pref_summary_delete_limit"
android:title="@string/pref_title_mms_delete" ... />
</PreferenceCategory>
...
</PreferenceScreen>
使用子屏幕(subscreens)
案例代码:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- opens a subscreen of settings -->
<PreferenceScreen
android:key="button_voicemail_category_key"
android:title="@string/voicemail"
android:persistent="false">
<ListPreference
android:key="button_voicemail_provider_key"
android:title="@string/voicemail_provider" ... />
<!-- opens another nested subscreen -->
<PreferenceScreen
android:key="button_voicemail_setting_key"
android:title="@string/voicemail_settings"
android:persistent="false">
...
</PreferenceScreen>
<RingtonePreference
android:key="button_voicemail_ringtone_key"
android:title="@string/voicemail_ringtone_title"
android:ringtoneType="notification" ... />
...
</PreferenceScreen>
...
</PreferenceScreen>
<font color=red face=STHupo>请注意:</font>上面的设置已经够日常使用
处理
读
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String syncConnPref = sharedPref.getString(SettingsActivity.KEY_PREF_SYNC_CONN, "");
监听
- findPreference里面的参数就是我们使用android:key所定义
- onPreferenceTreeClick Prefs tree点击时调用
注册方式:
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
设置监听方式:
SharedPreferences.OnSharedPreferenceChangeListener listener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// listener implementation
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
使用小案例
还原初始设置
PreferenceManager.setDefaultValues(Context, R.xml.advanced_preferences, false);
- false : 首次调用生效;(true 每次调用都生效)
面包原材料
- Develop Guide Settings 【主要.科学上网】
- Android Preference 设置全攻略 其实看这个也可以