能存储一下类型数据
- Boolean
- Float
- Int
- Long
- String
- String
Set
A few of the most common preferences are:
-
Shows an item with a checkbox for a setting that is either enabled or disabled. The saved value is a boolean (
true
if it's checked). - Opens a dialog with a list of radio buttons. The saved value can be any one of the supported value types (listed above).
-
Opens a dialog with an
EditText
widget. The saved value is aString
. - 要想每组有个分割线 就把子元素放到这个标签下面
-
<PreferenceCategory
CheckBoxPreference
ListPreference
EditTextPreference
监控配置变化的状态。注意的是貌似在onPreferenceChange执行结束之后才会修改sharePreference文件。返回值如果设置false的时候,checkbox不会修改!
public class SettingActivity extends PreferenceActivity implements OnPreferenceChangeListener {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
System.out.println("preference:"+preference.getKey()+" "+newValue);
// TODO Auto-generated method stub
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
//
System.out.println(prefs.getBoolean("开启声音", true));
return true;
}
}
自定义带图片PreferenceActivity
http://my.oschina.net/huangsm/blog/40027
自定义一个Perference
public class ImagePreference extends Preference {
private ImageView iv_perference_icon;
private int _id;
private OnClickListener mOnClickListener;
public ImagePreference(Context context,int id) {
super(context);
this._id = id;
//设置布局文件
setLayoutResource(R.layout.preference_list_item_layout);
}
//重绘
@Override
protected void onBindView(View view) {
super.onBindView(view);
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)
mOnClickListener.onClick(view);
else return;
}
});
}
public void setOnClickListener(OnClickListener mOnClickListener){
this.mOnClickListener = mOnClickListener;
}
}
PreferenceActivity会自动帮你保存配置值到 SharePreference中,下一次启动的时候,会自动帮你显示已经更改的状况。 使用如下方法读取当前文件中保存的值
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
//
System.out.println(prefs.getBoolean("开启声音", true));