这段时间在研究android平台上的开源项目——StandupTimer,这是由jwood所设计的一个较为简单android应用,用于控制会议时间,类似秒表倒计时。
PreferenceActivity#
PreferenceActivity是android提供的对系统信息和配置进行自动保存的Activity,它通过[SharedPreference](http://developer.android.com/reference/android/content/SharedPreferences.html)方式将信息保存在XML 文件当中。使用PreferenceActivity不需要我们对SharedPreference进行操作,系统会自动对Activity 的各种View上的改变进行保存(这个真是太赞了!)。
在android项目中添加一个 android xml 文件需要注意的是这次选择的是 Preference。而不是以往的Layout

这个文件是保存在 res /xml 路径下的。
PreferenceScreen xml#
preference下的View是有限的,只有下面几个:
- CheckBoxPreference:CheckBox选择项,对应的值的ture或flase
- EditTextPreference:输入编辑框,值为String类型,会弹出对话框供输入。
- ListPreference: 列表选择,弹出对话框供选择。
- Preference:只进行文本显示,需要与其他进行组合使用。
- PreferenceCategory:用于分组。
- RingtonePreference:系统玲声选择
更多关于 PreferenceScreen的介绍可以查看博客园上的一篇文章:[Android之PreferenceActivity](http://www.cnblogs.com/wservices/archive/2010/07/08/1773449.html)
<div>
<span style="color: #0000ff;"><?</span><span style="color: #ff00ff;">xml version=”1.0″ encoding=”utf-8″</span><span style="color: #0000ff;">?></span>
«/span>PreferenceScreen xmlns:android=”http://schemas.android.com/apk/res/android”>
«/span>CheckBoxPreference android:key=”sounds”
android:title=”@string/play_sounds” android:summary=”@string/play_sounds_summary”
android:defaultValue=”true”></CheckBoxPreference>
«/span>EditTextPreference android:key=”warning_time”
android:title=”@string/warning_time” android:summary=”@string/warning_time_summary”
android:defaultValue=”15″ android:inputType=”phone” android:digits=”0123456789″></EditTextPreference>
«/span>CheckBoxPreference android:key=”unlimited_participants”
android:title=”@string/unlimited_participants” android:summary=”@string/unlimited_participants_summary”
android:defaultValue=”false”></CheckBoxPreference>
«/span>CheckBoxPreference android:key=”variable_meeting_length”
android:title=”@string/variable_meeting_length” android:summary=”@string/variable_meeting_length_summary”
android:defaultValue=”false”></CheckBoxPreference>
</PreferenceScreen>
<div class="cnblogs_code_toolbar">
<span class="cnblogs_code_copy"><a style="color: green;" title="复制代码"></a></span>
</div>
android:key 唯一标识符。它对应保存的XML保存的配置文件中的节点的 name属性
<div>
<span style="color: #008000;">/**</span><span style="color: #008000;">
获取是否播放声音
*/
publicstaticboolean playSounds(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SOUNDS, SOUNDS_DEFAULT);
}
<span style="color: #008000;">/**</span><span style="color: #008000;">
设置播放的声音
* @param context 上下文
* @param value 是否播放
*/
publicstaticvoid setPlaySounds(Context context, boolean value) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(SOUNDS, value).commit();
}
<span style="color: #008000;">/**</span><span style="color: #008000;">
获取警告时间
* @param context 上下文
* @return 警告时间秒
*/
publicstaticint 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;
}
}
<span style="color: #008000;">/**</span><span style="color: #008000;">
设置警告时间
* @param context 上下文
* @param warningTime 警告时间
*/
publicstaticvoid setWarningTime(Context context, int warningTime) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(WARNING_TIME, Integer.toString(warningTime)).commit();
}
<span style="color: #008000;">/**</span><span style="color: #008000;">
参加人数无限制
* @param context
* @return
*/
publicstaticboolean allowUnlimitedParticipants(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(UNLIMITED_PARTICIPANTS, UNLIMITED_PARTICIPANTS_DEFAULT);
}
<span style="color: #0000ff;">public</span><span style="color: #0000ff;">static</span><span style="color: #0000ff;">void</span> setAllowUnlimitedParticipants(Context context, <span style="color: #0000ff;">boolean</span> value) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(UNLIMITED_PARTICIPANTS, value).commit();
}
<span style="color: #008000;">/**</span><span style="color: #008000;">
允许编辑会议时间
* @param context
* @return
*/
publicstaticboolean allowVariableMeetingLength(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(VARIABLE_MEETING_LENGTH, VARIABLE_MEETING_LENGTH_DEFAULT);
}
<span style="color: #0000ff;">public</span><span style="color: #0000ff;">static</span><span style="color: #0000ff;">void</span> setAllowVariableMeetingLength(Context context, <span style="color: #0000ff;">boolean</span> value) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(VARIABLE_MEETING_LENGTH, value).commit();
}
<div class="cnblogs_code_toolbar">
<span class="cnblogs_code_copy"><a style="color: green;" title="复制代码"></a></span>
</div></div>
getDefaultSharedPreferences(Context )用来获取preferences.以后的操作就和普通的Sharedpreferences一样了,如果需要修改某项配置的信息,记得最后需要 commit()。
当其他地方需要使用配置时,可以直接调用 setting.getXXX() 方法来获取配置信息。
💬 评论