Preference mainly implements some configuration data, some of the content we selected last time, we hope it will still be valid when the next application is called, without the need for users to configure or select again. Android provides the preference key-value pair to handle this situation. It automatically saves the data and takes effect immediately. Android also provides a similar layout method for Preference layout.
The Preference components include ListPreference, EditTextPreference, CheckBoxPreference, and SwitchPreference. Compared with ListView, EditText, CheckBox, Switch, and RingtonePreference in the View.
1. describes the attributes of the Preference component.
Android: key: Unique "ID" of each Preference control, which uniquely indicates this Preference.
Android: defaultValue: default value. For example, the default value of CheckPreference can be "true", and the default value is selected;
The default value of EditTextPreference can be "110 ".
Android: enabled: indicates whether the Preference is available.
Android: title: The title displayed by each Preference in the PreferenceScreen Layout-large title
Android: summary: The title displayed by each Preference in the PreferenceScreen Layout-Subtitles (not available)
Android: persistent: indicates whether the value corresponding to the Preference element is written to the sharedPreferen file. If it is true, it indicates writing
Otherwise, the value of the Preference element is not written.
Android: dependency: indicates that the available status of A Preference depends on another Preference (expressed by B ). B is available,
A is available; B is unavailable, and A is unavailable.
Android: disableDependentsState: Opposite to android: dependency. If B is available, A is unavailable. If B is unavailable, A is available.
Feature: ListPreference
Android: dialogTitle: The title displayed in the pop-up control dialog box
Android: entries: array type, control text to be displayed
Android: entryValues: array type, key-value pairs corresponding to the text, value is saved to the sharedPreference File
RingtonePreference
Android: ringtoneType: the type of the bell. The main types include: ringtone (music), notification (notification), and alarm (alarm)
And all (all available audio types ).
Android: showDefault: the default ringtone. You can use system (Boolean value --- true, false) or custom ringtones.
Android: showSilent: Specifies whether the ringtone is muted. Specified ringtones include system default ringtones or custom ringtones
2. The instance implements the control mentioned above
1> code directory structure
2>
3> xml-Related Files
A. xml-> setting_preference.xml
Preference xml files are not stored in res-> layout. Instead, you need to create a new xml folder and put setting_preference.xml in this xml Folder:
B. layout-> activity_main.xml
C. layout-> activity_about.xml
D. value-> array. xml
Red
Green
Bule
4>. java code implementation
A. Constant ConstantUtil. java
package com.example.settingdemo;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 = "check_box_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";}
B. MainActivity. java
Package com. example. settingdemo; import android. app. activity; import android. content. intent; import android. content. sharedPreferences; import android. OS. bundle; import android. preference. preferenceManager; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity implements OnClickListener {private Button settingBtn, showSettingInfoBtn; private TextView showInfoTv; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); 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) ;}@ Overridepublic void onResume () {super. onResume (); showSettingInfo ();}/*** display setting information */private void showSettingInfo () {SharedPreferences share = PreferenceManager. getdefasharsharedpreferences (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 switchInfo = share. getBoolean (ConstantUtil. SWITCH_KEY, true); String ringtoneInfo = share. getString (ConstantUtil. RING_TONE_KEY, "default ringtone"); info. append ("ListInfo :"). append (listInfo ). append ("\ n "). append ("editTextInfo :"). append (editTextInfo ). append ("\ n "). append ("checkBoxInfo :"). append (checkBoxInfo ). append ("\ n "). append ("switchInfo :"). append (switchInfo ). append ("\ n "). append ("ringtoneInfo :"). append (ringtoneInfo ). append ("\ n"); showInfoTv. setText (info. toString () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. setting_btn: Intent intent = new Intent (MainActivity. this, SettingActivity. class); startActivity (intent); break; case R. id. show_setting_info_btn: showSettingInfo (); break ;}}}
C. SettingActivity. java
Package com. example. settingdemo; import android. content. intent; import android. content. sharedPreferences; import android. content. sharedPreferences. onSharedPreferenceChangeListener; import android. OS. bundle; import android. preference. checkBoxPreference; import android. preference. editTextPreference; import android. preference. listPreference; import android. preference. preference; import android. preference. preference. onPreferenceClickListener; import android. preference. preferenceActivity; import android. preference. ringtonePreference; import android. preference. switchPreference; import android. widget. toast; public class SettingActivity extends implements values, attributes {private ListPreference mListPreference; private EditTextPreference mEditTextPreference; private CheckBoxPreference values; private SwitchPreference values; private Preference mPreference; private extends mRingtonePreference; @ SuppressWarnings ("deprecation") @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. addPreferencesFromResource (R. xml. setting_preference); initPreference () ;}@ SuppressWarnings ("deprecation") 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) ;}@ SuppressWarnings ("deprecation") @ Overridepublic void onResume () {super. onResume (); SharedPreferences share = this. getPreferenceScreen (). getSharedPreferences (); setDefaultSummary (share); share. registerOnSharedPreferenceChangeListener (this) ;}@ SuppressWarnings ("deprecation") @ Overridepublic void onPause () {super. onPause (); this. getPreferenceScreen (). getSharedPreferences (). unregisterOnSharedPreferenceChangeListener (this);}/*** set the component title * @ param share */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, "default ringtone");} @ Overridepublic void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {// TODO Auto-generated method stubif (ConstantUtil. LIST_KEY.equals (key) {mListPreference. setSummary (mListPreference. getEntry ();} else if (ConstantUtil. EDIT_TEXT_KEY.equals (key) {mEditTextPreference. setSummary (sharedPreferences. getString (key, "this is editTextPreference");} else if (ConstantUtil. RING_TONE_KEY.equals (key) {mRingtonePreference. setSummary (sharedPreferences. getString (key, "default ringtone") ;}@ Overridepublic boolean onPreferenceClick (Preference preference) {// TODO Auto-generated method stubif (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, "click MyPreference", Toast. LENGTH_SHORT ). show () ;}return false ;}}
D. Jump to the AboutActivity. java page
package com.example.settingdemo;import android.app.Activity;import android.os.Bundle;public class AboutActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_about);}}
5>. List AndroidManifest. xml
So far, this article has been completed. You can try it.
Source code download link: http://download.csdn.net/detail/a123demi/7564135