StandupTimer 之 PreferenceActivity

 

PreferenceScreen xml

  preference下的View是有限的,只有下面几个:
  • 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 弹出的对话框中的标题信息

文本类型,多为大写、小写和数字符号。
    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() 方法来获取配置信息。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值