这是简单的配置acitvit
public class SmsPopupConfigActivity extends PreferenceActivity {
}
addPreferencesFromResource(R.xml.preferences);
相当于setcontentview,add方法自动生成view
初始化一个按钮:
// Button 1 preference
button1 =
(ButtonListPreference) findPreference(getString(R.string.pref_button1_key));
button1.refreshSummary();
button1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
quickReplyPref.setChecked(
isQuickReplyActive((String) newValue, button2.getValue(), button3
.getValue()));
updateReplyTypePref((String) newValue, button2.getValue(), button3.getValue());
return true;
}
});
// 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences addPreferencesFromResource(R.xml.preference); mContext = this; //CheckBoxPreference组件 CheckBoxPreference mCheckbox0 = (CheckBoxPreference) findPreference("checkbox_0"); mCheckbox0.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { //这里可以监听到这个CheckBox 的点击事件 return true; } }); mCheckbox0.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference arg0, Object newValue) { //这里可以监听到checkBox中值是否改变了 //并且可以拿到新改变的值 Toast.makeText(mContext, "checkBox_0改变的值为" + (Boolean)newValue, Toast.LENGTH_LONG).show(); return true; } }); }xml格式如下:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="CheckBoxPreference"> <CheckBoxPreference android:key="checkbox_0" android:title="CheckBox_A" android:summary="这是一个勾选框A" > </CheckBoxPreference> </PreferenceCategory> </PreferenceScreen>
参考:
http://www.cnblogs.com/ligang305/archive/2012/08/08/2627715.html
http://blog.csdn.net/ywtcy/article/details/7895814
项目:net.everythingandroid.smspopup