第一个activity只有两个按钮,一个是跳转到设置界面的按钮,一个是点击显示配置界面内容的按钮。
设置界面里写了各种Preference的用法:
public class PreferenceDemoActivity extends AppCompatActivity implements View.OnClickListener { private Button settingBtn,showSettingInfoBtn; private TextView showInfoTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_preference_demo); settingBtn = (Button)this.findViewById(R.id.setting_btn); showSettingInfoBtn = (Button)this.findViewById(R.id.show_setting_info_btn); showInfoTv = (TextView)this.findViewById(R.id.show_info_tv); settingBtn.setOnClickListener(this); showSettingInfoBtn.setOnClickListener(this); } public void onResume(){ super.onResume(); showSettingInfo(); } private void showSettingInfo(){ SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(this); StringBuffer info = new StringBuffer(); String listInfo = share.getString(ConstantUtil.LIST_KEY,"green"); String editTextInfo = share.getString(ConstantUtil.EDIT_TEXT_KEY,"this is editTextPreference"); boolean checkboxInfo = share.getBoolean(ConstantUtil.CHECK_BOX_KEY,true); boolean swithInfo = share.getBoolean(ConstantUtil.SWITCH_KEY,true); String ringtoneInfo = share.getString(ConstantUtil.RING_TONE_KEY, "默认铃声"); String alarmInfo = share.getString(ConstantUtil.DATE_PICKER,"日期"); info.append("listInfo:").append(listInfo).append("\n") .append("editTextInfo:").append(editTextInfo).append("\n") .append("checkboxInfo:").append(checkboxInfo).append("\n") .append("switchInfo:").append(swithInfo).append("\n") .append("ringtoneInfo:").append(ringtoneInfo).append("\n") .append("dateInfo:").append(alarmInfo).append("\n"); showInfoTv.setText(info.toString()); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.setting_btn: Intent intent = new Intent(PreferenceDemoActivity.this,SettingActivity.class); startActivity(intent); break; case R.id.show_setting_info_btn: showSettingInfo(); break; } }
}
布局文件:
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.h.mynote.demo.preference.PreferenceDemoActivity"> <Button android:id="@+id/setting_btn" android:text="设置" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/show_setting_info_btn" android:text="显示设置信息" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/show_info_tv" android:textSize="20sp" android:layout_width="fill_parent" android:layout_height="wrap_content" /> LinearLayout>设置界面:
public class SettingActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener,Preference.OnPreferenceClickListener { private ListPreference mListPreference; private EditTextPreference mEditTextPreference; private CheckBoxPreference mCheckBoxPreference; private SwitchPreference mSwitchPreference; private Preference mPreference; private RingtonePreference mRingTonePreference; private AlarmPreference alarmPreference; int year,month,day; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); this.addPreferencesFromResource(R.xml.remind_detail); initPreference(); } private void initPreference(){ mListPreference = (ListPreference)this.findPreference(ConstantUtil.LIST_KEY); mEditTextPreference = (EditTextPreference)this.findPreference(ConstantUtil.EDIT_TEXT_KEY); mCheckBoxPreference = (CheckBoxPreference)this.findPreference(ConstantUtil.CHECK_BOX_KEY); mSwitchPreference = (SwitchPreference)this.findPreference(ConstantUtil.SWITCH_KEY); mPreference = (Preference)this.findPreference(ConstantUtil.PREFERENCE_KEY); mRingTonePreference = (RingtonePreference)this.findPreference(ConstantUtil.RING_TONE_KEY); mPreference.setOnPreferenceClickListener(this); alarmPreference =(AlarmPreference)this.findPreference(ConstantUtil.DATE_PICKER); alarmPreference.setOnPreferenceClickListener(this); } public void onResume(){ super.onResume(); SharedPreferences share = this.getPreferenceScreen().getSharedPreferences(); boolean isCheck = share.contains(ConstantUtil.DATE_PICKER); Toast.makeText(SettingActivity.this,isCheck+"====date",Toast.LENGTH_SHORT).show(); setDefaultSummary(share); share.registerOnSharedPreferenceChangeListener(this); } public void onPause(){ super.onPause(); this.getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); } //设置组件小标题 private void setDefaultSummary(SharedPreferences share){ mListPreference.setSummary(share.getString(ConstantUtil.LIST_KEY,"Green")); mEditTextPreference.setSummary(share.getString(ConstantUtil.EDIT_TEXT_KEY,"this is editTextPreference")); mRingTonePreference.setSummary(share.getString(ConstantUtil.RING_TONE_KEY,"默认铃声")); //alarmPreference.setSummary(share.getString(ConstantUtil.DATE_PICKER,"日期")); } @Override public boolean onPreferenceClick(Preference preference) { if(ConstantUtil.PREFERENCE_KEY.equals(preference.getKey())){ Intent intent = new Intent(SettingActivity.this,AboutActivity.class); startActivity(intent); }else if(ConstantUtil.MY_PREFERENCE_KEY.equals(preference.getKey())){ Toast.makeText(this,"点击MyPreference",Toast.LENGTH_SHORT).show(); }else if(ConstantUtil.DATE_PICKER.equals(preference.getKey())){ Calendar myCal = Calendar.getInstance(Locale.CHINA); year = myCal.get(Calendar.YEAR); month = myCal.get(Calendar.MONTH); day = myCal.get(Calendar.DAY_OF_MONTH); DatePickerDialog dpd = new DatePickerDialog(SettingActivity.this,viewOnDateSetListner,year,month,day); dpd.show(); } return false; } private DatePickerDialog.OnDateSetListener viewOnDateSetListner = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { year = year; month = monthOfYear; day = dayOfMonth; alarmPreference.setSummary(year+"/"+month+"/"+day); } }; @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if(ConstantUtil.LIST_KEY.equals(key)){ mListPreference.setSummary(mListPreference.getEntry()); }else if(ConstantUtil.EDIT_TEXT_KEY.equals(key)){ mEditTextPreference.setSummary(sharedPreferences.getString(key,"this is editText")); }else if(ConstantUtil.RING_TONE_KEY.equals(key)){ mRingTonePreference.setSummary(sharedPreferences.getString(key,"默认铃声")); }else if(ConstantUtil.DATE_PICKER.equals(key)){ Toast.makeText(SettingActivity.this,"11111",Toast.LENGTH_SHORT).show(); alarmPreference.setSummary(sharedPreferences.getString(key,"默认日期")); } } }设置界面布局:
xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="第一项"> <ListPreference android:defaultValue="Green" android:dialogTitle="请选择您需要的颜色" android:entries="@array/my_color_array" android:entryValues="@array/my_color_array" android:key="list_key" android:summary="Green" android:title="颜色设置"/> PreferenceCategory> <PreferenceCategory android:title="第二项"> <EditTextPreference android:defaultValue="请输入描述" android:key="edit_text_key" android:summary="this is editTextPreference" android:title="文本输入框"/> PreferenceCategory> <PreferenceCategory android:title="第三项"> <CheckBoxPreference android:key="checkbox_key" android:defaultValue="true" android:title="checkbox"/> <SwitchPreference android:key="switch_key" android:defaultValue="true" android:title="switch"/> <Preference android:key="preference_key" android:title="关于"/> PreferenceCategory> <PreferenceCategory android:title="第四项"> <RingtonePreference android:key="ring_tone_key" android:title="Ringtone" android:summary="默认铃声" android:ringtoneType="alarm"> RingtonePreference> PreferenceCategory> <PreferenceCategory android:title="第五项"> <com.h.mynote.demo.remind.AlarmPreference android:key="date_key" android:icon="@drawable/icon_zdy_date" android:defaultValue="日期" android:title="日期" android:summary="2016/3/24"> com.h.mynote.demo.remind.AlarmPreference> PreferenceCategory> PreferenceScreen>定义了各种常量的配置文件:
public class ConstantUtil { public static final String LIST_KEY = "list_key"; public static final String EDIT_TEXT_KEY = "edit_text_key"; public static final String CHECK_BOX_KEY = "checkbox_key"; public static final String SWITCH_KEY = "switch_key"; public static final String RING_TONE_KEY = "ring_tone_key"; public static final String PREFERENCE_KEY = "preference_key"; public static final String MY_PREFERENCE_KEY = "my_preference_key"; public static final String DATE_PICKER = "date_key"; }