利用PreferenceFragment 记录用户喜好,当用户打开程序时,首先弹出对话框(该PreferenceFragment 加载的layout),进入个人喜好,个人信息输入界面,
当填完信息后,所有的数据保存在/data/data/程序名/shared_prefs/xxxx_preferences.xml里面。 可以单独写一个preference的类,当程序打开时,利用该preference
去xxxx_preferences.xml文件里面读取相应的数据,然后存储在类里面,这样提高读数据的时间。
一. 利用Fragment的replace()函数加载PreferenceFragment
getFragmentManager().beginTransaction().replace(R.id.main_frame_container, new SettingsFragment(), SettingsFragment.class.getName())
.addToBackStack(SettingsFragment.class.getName()).commit();
关于replace()函数的参数
1. 第一个参数是要加载到的区域,虽然是函数名是replace,但是这个区域被替代前可以不是fragment.
2. 第二个参数是要加载的fragment
3. 第三个参数是这个fragment的ID, 便于以后查找fragment.
二.创建PreferenceFragement 类
1. override the function onCreate()函数,添加layout, 给layout中的sub-fragment 添加listener事件。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource, 加载布局文件
addPreferencesFromResource(R.xml.preference);
//add listener and set summary as the default value from /data/date/....xxx.xml
//the summary is the display value
//给sub-fragment(提示控件)添加监听事件,并且从/data/data/程序名/shared_prefs/xxxx_preferences.xml读取default的值
//修改summary(显示的值)英default的值
bindPreferenceSummaryToValue(findPreference(HiSettingsHelper.PERF_USERNAME));
bindPreferenceSummaryToValue(findPreference(HiSettingsHelper.PERF_SECQUESTION));
bindPreferenceSummaryToValue(findPreference(HiSettingsHelper.PERF_TAILTEXT));
bindPreferenceSummaryToValue(findPreference(HiSettingsHelper.PERF_TAILURL));
bindPreferenceSummaryToValue(findPreference(HiSettingsHelper.PERF_BLANKLIST_USERNAMES));
bindPreferenceSummaryToValue(findPreference(HiSettingsHelper.PERF_TEXTSIZE_POST_ADJ));
}
1.1 关于布局文件。创建xml文件夹,然后创建preference.xml文件。 在文件中定义了EditTextPreference, ListPreference, SwitchPreferecn, and DialogPreference.
所有的preference都包含在PreferenceScreen里面,可以用PreferenceCategory来分块。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/pref_category_account" >
<EditTextPreference
android:inputType="text"
android:key="PERF_USERNAME"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_username" />
<EditTextPreference
android:inputType="textPassword"
android:key="PERF_PASSWORD"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_password" />
<EditTextPreference
android:inputType="text"
android:key="PERF_SECANSWER"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_login_answer" />
<ListPreference
android:defaultValue="0"
android:entries="@array/pref_login_question_list_titles"
android:entryValues="@array/pref_login_question_list_values"
android:key="PERF_SECQUESTION"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_login_question" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_tail" >
<SwitchPreference
android:defaultValue="true"
android:key="PERF_ADDTAIL"
android:title="@string/pref_title_add_tail" />
<EditTextPreference
android:defaultValue="@string/default_tail_text"
android:inputType="text"
android:key="PERF_TAILTEXT"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_tail_text" />
<EditTextPreference
android:defaultValue="@string/default_tail_url"
android:inputType="text"
android:key="PERF_TAILURL"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_tail_url" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_ui" >
<SwitchPreference
android:defaultValue="false"
android:key="PERF_EINK_OPTIMIZATION"
android:title="@string/pref_title_eink_optimization" />
<ListPreference
android:defaultValue="0"
android:entries="@array/pref_post_textsize_adjust_titles"
android:entryValues="@array/pref_post_textsize_adjust_values"
android:key="PERF_TEXTSIZE_POST_ADJ"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_post_textsize_adjust_title" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_general" >
<SwitchPreference
android:defaultValue="false"
android:key="PERF_ENCODEUTF8"
android:title="@string/pref_title_encode_utf8" />
<EditTextPreference
android:inputType="text"
android:key="PERF_BLANKLIST_USERNAMES"
android:maxLines="1"
android:selectAllOnFocus="false"
android:singleLine="true"
android:title="@string/pref_title_blanklist" />
<net.jejer.hipda.ui.AboutDialogPreference
android:summary="@string/app_versionName"
android:title="@string/pref_title_about" />
</PreferenceCategory>
</PreferenceScreen>
<1>关于EditTextPreference
inputType: 输入类型,如果是textPassword, 用户输入一个字符后,后变成黑点。如果是phone,会自动调出数字键盘
key:是该preference的indicator, 也是通过该值到xxx_preference.xml里面查找相应的变量值。
title: 弹出来的控件的标题。
<2>ListPreference
entries: 弹出的List中每个Item显示的值,必须是string-array
entryValues: 弹出的List中每个Item的标号
<3>SwitchPreference
defaultValue: 默认的值
<4>这里自己定义了一个DialogPreference
a. override 函数onCreateDialogView()来加载layout, 改layout只用webview这个控件。
protected View onCreateDialogView () {
Log.v(LOG_TAG, "onCreateDialogView");
//return null;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.dialog_about, null);
}
b. override 函数onBindDialogView()来显示view的信息
@Override
protected void onBindDialogView (View view) {
Log.v(LOG_TAG, "onBindDialogView");
WebView wv = (WebView)view;
wv.loadUrl("file:///android_asset/html/about.html");
}
这里贴出整个类的code
public class AboutDialogPreference extends DialogPreference {
private final String LOG_TAG = getClass().getSimpleName();
public AboutDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected View onCreateDialogView () {
Log.v(LOG_TAG, "onCreateDialogView");
//return null;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.dialog_about, null);
}
@Override
protected void onBindDialogView (View view) {
Log.v(LOG_TAG, "onBindDialogView");
WebView wv = (WebView)view;
wv.loadUrl("file:///android_asset/html/about.html");
}
}
2.1 类 函数findPreference(HiSettingsHelper.PERF_USERNAME)
是在此PreferenceFragment加载的preference群中查找, 通过key值来查找。
2.2 自定义函数bindPreferenceSummaryToValue(Preference);
用于为sub-preference添加监听事件,并把summary的值修改为default 值(从xxxx_preference.xml读取的值)
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference
.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
if (preference instanceof CheckBoxPreference) {
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getBoolean(preference.getKey(),
false));
} else if (preference instanceof SwitchPreference) {
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getBoolean(preference.getKey(),
false));
} else {
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getString(preference.getKey(),
""));
}
}2.3 利用Preference.OnPreferenceChangeListener作为listenner
由于ListPreference 和其他的preference的setsummary()函数不一样,所以分开写。
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference
.setSummary(index >= 0 ? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
本文介绍如何利用PreferenceFragment记录用户喜好,详细阐述了如何创建并加载布局文件,以及如何设置监听事件和从XML文件中读取默认值。用户在PreferenceFragment中输入的个人信息会被保存到/data/data/程序名/shared_prefs/xxxx_preferences.xml,之后可以通过自定义的preference类读取和管理这些数据,提高数据读取效率。

5724

被折叠的 条评论
为什么被折叠?



