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 弹出的对话框中的标题信息
文本类型,多为大写、小写和数字符号。
android:inputType="none"
android:inputType="text"
android:inputType="textCapCharacters"
android:inputType="textCapWords"
android:inputType="textCapSentences"
android:inputType="textAutoCorrect"
android:inputType="textAutoComplete"
android:inputType="textMultiLine"
android:inputType="textImeMultiLine"
android:inputType="textNoSuggestions"
android:inputType="textUri"
android:inputType="textEmailAddress"
android:inputType="textEmailSubject"
android:inputType="textShortMessage"
android:inputType="textLongMessage"
android:inputType="textPersonName"
android:inputType="textPostalAddress"
android:inputType="textPassword"
android:inputType="textVisiblePassword"
android:inputType="textWebEditText"
android:inputType="textFilter"
android:inputType="textPhonetic"
数值类型
android:inputType="number"
android:inputType="numberSigned"
android:inputType="numberDecimal"
android:inputType="phone"//拨号键盘
android:inputType="datetime"
android:inputType="date"//日期键盘
android:inputType="time"//时间键盘
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() 方法来获取配置信息。