PreferenceScreen xml
- CheckBoxPreference:CheckBox选择项,对应的值的ture或flase
- EditTextPreference:输入编辑框,值为String类型,会弹出对话框供输入。
- ListPreference: 列表选择,弹出对话框供选择。
- Preference:只进行文本显示,需要与其他进行组合使用。
- PreferenceCategory:用于分组。
- RingtonePreference:系统玲声选择
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:defaultValue="true"
android:key="sounds"
android:summary="@string/play_sounds_summary"
android:title="@string/play_sounds" />
<EditTextPreference
android:defaultValue="15"
android:digits="0123456789"
android:inputType="phone"
android:key="warning_time"
android:summary="@string/warning_time_summary"
android:title="@string/warning_time" />
<CheckBoxPreference
android:defaultValue="false"
android:key="unlimited_participants"
android:summary="@string/unlimited_participants_summary"
android:title="@string/unlimited_participants" />
<CheckBoxPreference
android:defaultValue="false"
android:key="variable_meeting_length"
android:summary="@string/variable_meeting_length_summary"
android:title="@string/variable_meeting_length" />
</PreferenceScreen>
Preference:只进行文本显示,需要与其他进行组合使用
PreferenceCategory:用于分组
ListPreference: 列表选择,弹出对话框供选择
PreferenceScreen:PreferenceActivity的根元素,必须为它
EditTextPreference:输入编辑框,值为String类型,会弹出对话框供输入
CheckBoxPreference:CheckBox选择项,对应的值的ture或flase
RingtonePreference:系统玲声选择。
key:唯一标识.
title:显示标题(大字体显示)
summary:副标题(小字体显示)
summaryOn和summaryOff参数来设置控件选中和未选中时的提示
defaultValue:默认值
entries: Reference to an array resource that will populate the ListView(列表中显示的值。为一个数组,通读通过资源文件进行设置)
entryValues :与android:entries相对应的值
dialogTitle 弹出的对话框中的标题信息
文本类型,多为大写、小写和数字符号。
数值类型
Prefs代码(PreferenceActivity)
当PreferenceActivity上的View有所更改时,系统会自动将对应的值更新到XML配置文件中,该文件可以在android 的 file explorer 中的 data/data/"yourPageName"/shared_prefs/"yourpageName"_prefenrences.xml中找到。“yourpageName”表示你项目所在的包。
public class Prefs extends PreferenceActivity {
private static final String SOUNDS = "sounds";
private static final boolean SOUNDS_DEFAULT = true;
private static final String WARNING_TIME = "warning_time";
private static final int WARNING_TIME_DEFAULT = 15;
private static final String UNLIMITED_PARTICIPANTS = "unlimited_participants";
private static final boolean UNLIMITED_PARTICIPANTS_DEFAULT = false;
private static final String VARIABLE_MEETING_LENGTH = "variable_meeting_length";
private static final boolean VARIABLE_MEETING_LENGTH_DEFAULT = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);/* 加载资源文件 */
}
/** * 获取是否播放声音 */
public static boolean playSounds(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(SOUNDS, SOUNDS_DEFAULT);
}
/**
*设置播放的声音 *
* @param context 上下文
* @param value 是否播放
*/
public static void setPlaySounds(Context context, boolean value) {
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putBoolean(SOUNDS, value).commit();
}
/** * 获取警告时间
* @param context 上下文
* @return 警告时间秒
**/
public static int getWarningTime(Context context) {
String value = PreferenceManager
.getDefaultSharedPreferences(context)
.getString(WARNING_TIME, Integer.toString(WARNING_TIME_DEFAULT));
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
setWarningTime(context, WARNING_TIME_DEFAULT);
return WARNING_TIME_DEFAULT;
}
}
/** * 设置警告时间
* @param context 上下文
* @param warningTime 警告时间
*/
public static void setWarningTime(Context context, int warningTime) {
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString(WARNING_TIME, Integer.toString(warningTime))
.commit();
}
/**
* 获取参加人数无限制
*/
public static boolean allowUnlimitedParticipants(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(UNLIMITED_PARTICIPANTS,
UNLIMITED_PARTICIPANTS_DEFAULT);
}
/**
* 设置参加人数限制
* @param context
* @param value
*/
public static void setAllowUnlimitedParticipants(Context context,
boolean value) {
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putBoolean(UNLIMITED_PARTICIPANTS, value).commit();
}
/**
* 获取允许编辑会议时间
*/
public static boolean allowVariableMeetingLength(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(VARIABLE_MEETING_LENGTH,
VARIABLE_MEETING_LENGTH_DEFAULT);
}
/**
* 设置允许编辑会议时间
* @param context
* @param value
*/
public static void setAllowVariableMeetingLength(Context context,
boolean value) {
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putBoolean(VARIABLE_MEETING_LENGTH, value).commit();
}
}
getDefaultSharedPreferences(Context )用来获取preferences.以后的操作就和普通的Sharedpreferences一样了,如果需要修改某项配置的信息,记得最后需要 commit()。
当其他地方需要使用配置时,可以直接调用 setting.getXXX() 方法来获取配置信息。