PreferenceActivity是android提供的对系统信息和配置进行自动保存的Activity,它通过SharedPreference方式将信息保存在XML 文件当中。使用PreferenceActivity不需要我们对SharedPreference进行信息保存读取操作,系统会自动对Activity 的各种View上的改变进行保存()。
可以自己实现一个,但是那样又会使Activity中的逻辑代码和xml布局文件过于复杂,远远不及PreferenceActivity来的方便快捷。
用他来自定义设置,省去了我们通过对SharedPreference设置的保存读取,很方便。PreferenceActivity在每次退出后都会自动记录下当前页面的设置,下次启动时会自动加载
这是根据hack004修改来的
一、效果图:
二、部分自定义实现类
自定义Prefeference
把view的点击事件传递给ImagePreference
package com.manning.androidhacks.hack004.preference;
import com.manning.androidhacks.hack004.R;
import android.content.Context;
import android.preference.Preference;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class ImagePreference extends Preference {
private ImageView iv_perference_icon;
private int _id;
// 声明监听器
private OnClickListener mOnClickListener;
public ImagePreference(Context context, int id) {
super(context);
// TODO Auto-generated constructor stub
this._id = id;
setLayoutResource(R.layout.preference_list_item_layout);
}
@Override
protected void onBindView(View view) {
// TODO Auto-generated method stub
super.onBindView(view);
if (iv_perference_icon == null) {
iv_perference_icon = (ImageView) view.findViewById(R.id.iv_perference_icon);
iv_perference_icon.setImageResource(_id);
view.setClickable(true);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (mOnClickListener != null)
// 把view的点击事件传递给ImagePreference
mOnClickListener.onClick(view);
else
return;
}
});
}
}
// 设置ImagePreference的监听方法
public void setOnClickListener(OnClickListener mOnClickListener) {
this.mOnClickListener = mOnClickListener;
}
}
三、主类;
import com.manning.androidhacks.hack004.preference.ImagePreference;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
//MainActivit继承PreferenceActivity,其是ListActivity的子类
public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置背景
this.getListView().setBackgroundResource(R.drawable.backgroud);
addPreferencesFromResource(R.xml.prefs);
initImagePreference();
Preference sharePref = findPreference("pref_share");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Check this app!");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Check this awesome app at: ...");
sharePref.setIntent(shareIntent);
Preference ratePref = findPreference("pref_rate");
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
ratePref.setIntent(goToMarket);
updateUserText();
}
private void initImagePreference() {
getPreferenceScreen().setOrderingAsAdded(true);
ImagePreference pre_update = new ImagePreference(this, R.drawable.update);
// pre_update.setKey(Intents.KEY_UPDATE);
pre_update.setTitle("更新");
pre_update.setOrder(4);
pre_update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// You can do something here when somebady click this view.
Toast.makeText(MainActivity.this, "版本升级", 1).show();
}
});
getPreferenceScreen().addPreference(pre_update);
}
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("pref_username")) {
updateUserText();
}
}
private void updateUserText() {
EditTextPreference pref;
pref = (EditTextPreference) findPreference("pref_username");
String user = pref.getText();
if (user == null) {
user = "请填写你的用户名";
}
pref.setSummary(String.format("Username: %s", user));
}
}
五、布局文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="pref_first_preferencescreen_key"
android:title="设置界面">
<PreferenceCategory
android:title="User">
<EditTextPreference
android:key="pref_username"
android:summary="Username:"
android:title="用户名"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Application">
<Preference
android:key="pref_rate"
android:summary="Rate the app in the store!"
android:title="在市场下载率"/>
<Preference
android:key="pref_share"
android:summary="Share the app with your friends"
android:title="分享"/>
<com.manning.androidhacks.hack004.preference.EmailDialog
android:dialogIcon="@drawable/ic_launcher"
android:dialogTitle="Send Feedback"
android:dialogMessage="Do you want to send an email with feedback?"
android:key="pref_sendemail_key"
android:negativeButtonText="Cancel"
android:positiveButtonText="OK"
android:summary="Send your feedback by e-mail"
android:title="发送邮件"/>
<com.manning.androidhacks.hack004.preference.AboutDialog
android:dialogIcon="@drawable/ic_launcher"
android:dialogTitle="About"
android:key="pref_about_key"
android:negativeButtonText="@null"
android:title="关于"/>
</PreferenceCategory>
</PreferenceScreen>
AboutDialog,EmailDialog为自定义控件
六、源码: