Android Preference usage
Android Preference is often used in functions such as setting. Android provides the preference key-value pair to handle this situation and automatically saves the data and takes effect immediately, this is saved using the android sharedpreferences method, which does not need to be manually written, the following figure uses Preference to save and add a simple Preference. We can add an xml directory under the res directory to add a itchq file, this file is equivalent to copying the code of our Preference layout file <? Xml version = "1.0" encoding = "UTF-8"?> <PreferenceScreen xmlns: android = "http://schemas.android.com/apk/res/android"> <CheckBoxPreference android: key = "checkbox" android: title = "CheckBoxPreference" android: summary = "check it box"/> <RingtonePreference android: key = "ring" android: title = "ringtone" android: summary = "check a ringtone"/> </PreferenceScreen> copy the Code. This is itchq. the code in xml is described below. Let's take a look at how to use the copy code package com in that Activity. itchq. itchqpreference; import com. itchq. itchqpreference. r; import android. OS. bundle; import android. preference. checkBoxPreference; import android. preference. preferenceActivity; import android. preference. ringtonePreference; public class ItChqActivity extends PreferenceActivity {@ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated m Ethod stub super. onCreate (savedInstanceState); addPreferencesFromResource (R. xml. itchq) ;}} copy the code above the Activity code. This Activity does not inherit the Activity class but PreferenceActivity. In fact, this field indicates the inheritance subclass of the Activity, similar to Activity, setContentView () is used in Activity to load a layout file. Here addPreferencesFromResource () is used to load the layout file, since it is an Activity, do not forget it in AndroidManifast. add the corresponding configuration in xml. The general xml layout attributes of preference are in the preference layout file. We can often use the following attributes, which are prefer Ence controls are common to each other. Android: key is equivalent to android: id. This key is used to uniquely indicate the title of the Preference android: title Preference. android: summary indicates the Preference prompt, which is smaller than android: title and is displayed on android: under the title, android: defaultValue indicates the default value. For example, you can specify whether the default value is "true" or "false" android: enabled indicates whether the Preference is available. PreferenceScreen preferenceScreen is simply the root node of all Preference elements. It can be seen as the starting element of each PrederenceActivity layout, of course, this PreferenceScreen can also be used as a component, and other components can be nested in it, so this will be displayed on another Screen When we click a new screen copy Code <PreferenceScreen xmlns: android = "http://schemas.android.com/apk/res/android"> <CheckBoxPreference android: key = "checkbox" android: title = "CheckBoxPreference" android: summary = "check it box"/> <RingtonePreference android: key = "ring" android: title = "ringtone" android: summary = "check a ringtone"/> <PreferenceScreen android: key = "screen" android: title = "PreferenceScreen" android: sum Mary = "screen it"> <SwitchPreference android: key = "switch" android: title = "switchPreference" android: summary = "switch it"/> <EditTextPreference android: key = "edit" android: title = "editPreference" android: summary = "OK"/> </PreferenceScreen> the copied code contains a PreferenceScreen, switchPreference and EditTextPreference are nested in the PreferenceScreen. android: key is equivalent to android: id. Then we can find the corresponding component in the Activity, and Roid: title is the title, android: summary is the prompt, and the three attributes are the most basic information. See: the last item in the picture above is what we added, "PreferenceScreen" is our android: title, and "screen it" is the android: summary. When we click this option, a new page is displayed, as shown in: this is the two components nested in PreferenceScreen. SwitchPreference and EditTextPreference PreferenceCategory are similar to LinearLayou and RelativeLayout. They are used to combine a set of Preference and make the layout look hierarchical, beautiful copy Code <PreferenceCategory android: key = "category" andro Id: title = "PreferenceCategory"> <EditTextPreference android: key = "edit" android: title = "category" android: summary = "OK"/> </PreferenceCategory> copy the Code. For example, the PreferenceCategory effect is used when there is a large line before Category. The Preference control is briefly introduced here. This is the same as our TextView, that is, to display a text ListPreference. This is to display a considerable list, read the following code to copy the Code <ListPreference android: key = "list" android: title = "Options" android: entries = "@ array/entry_list" android: entryValues = "@ array/values_list" android: dialogTitle = "list dialogtitle" android: defaultValue = "1"/> copy the code and we can see android: the entries attribute indicates the text to be displayed by the listView dialog box control. The android: entryValues corresponds to the k Key-value pairs, value is saved to the sharedPreference file, while the array used by the entries and entryValue attributes is defined in the resource file arrays. xml array name. Let's take a look at arrays. definition copy code of the xml file <resources> <string-array name = "entry_list"> <item> total </item> <item> check </item> <item> airplace </item> </string-array> <string-array name = "values_list"> <item> 0 </item> <item> 1 </item> <item> 2 </item> </string-array> </resources> copy the Code. This is arrays. xml file definition. android: dialogTitle refers to the text in the list pop-up box, Android: defauleValues indicates which value is displayed by default. This value corresponds to the value of values_list. Let's take a look at the effect. The Options is the ListPreference, when we click this button, the display effect is shown as follows: we can see the following dialog form to display the listView. The default android: defaultValues = "1" corresponds to the second "check ", this title is the CheckBoxPreference checkBoxPreference set by android: dialogTitle. This is equivalent to checkBox. It is a multiple-choice option. First, read the code to copy the Code <CheckBoxPreference android: key = "checkbox_one" android: summaryOn = "On_check_one" android: summa RyOff = "offcheck_one"/> <CheckBoxPreference android: key = "checkbox_two" android: summaryOff = "offcheck_two" android: summaryOn = "On_check_two" android: title = "checbox_two"/> <CheckBoxPreference android: key = "checkbox_three" android: summaryOff = "offcheck_three" android: summaryOn = "On_check_three" android: title = "checbox_three"/> copy the code android: summaryOn and android: summaryOff to indicate the summary displayed separately in the selection and not selection. Run the following command: When we select checkb Ox_one is the following offcheck_one prompt and will be changed to On_check_one EditTextPreference equivalent to EditText. The related code is shown as follows: <EditTextPreference android: key = "edit" android: title = "Set Package Name" android: summary = "Set the Package Name" android: dialogTitle = "Package Name: "/> when we click" Set Package Name "below, a text dialog box will pop up." Package Name "is the android: the RingtonePreference attribute of dialogTitle is a function of the ringtone option. It is rarely used to describe the implementation of MultiSelectListPreference and ListPrefe. Rence is similar. The difference is that there can be no choice or multiple choices. The Code is the same as ListPreference, and the running effect is as follows: some of the above basic Preference controls have almost been introduced, next we will introduce how to use the click and storage functions: first, we will introduce how to find our control in our PreferenceActivity. In Activity, we use findViewById () in PreferenceActivity, we can use the findPreference method to find the specified method. The usage is as follows: mCheck = (CheckBoxPreference) findPreference ("checkbox "); mRing = (RingtonePreference) findPreference ("ring"); the parameters in findPreference are defined by android: key. Easy to understand. In the PreferenceActivity method, the method of an important listener click event is to copy the code @ Override public boolean onPreferenceTreeClick (PreferenceScreen preferenceScreen, Preference preference) {// TODO Auto-generated method stub return true;} copy the code parameter preference to indicate the clicked object. You can override this method when inheriting PreferenceActivity, to capture Preference events, use the following copy code @ Override public boolean onPreferenceTreeClick (PreferenceScreen preferenceScreen, Preference preference ){ // TODO Auto-generated method stub if (preference. getKey (). equals ("checkbox") {Log. I ("itchq", "checkbox");} return true;} the copied code has two important interfaces besides the method of the above listener: onPreferenceChangeListener triggers this event when the Preference element value changes. Remember that this event is triggered only when the element value changes, if the returned value is true, the new value is written to the sharedPreference file. If the value is false, the new value is not written to the sharedPreference file @ Override public boolean onPreferenceChange (Preference preference, Object newValue) {// TODO Auto-generated Method stub return false;} OnPreferenceClickListener is triggered when a control is clicked. You can perform the corresponding operation @ Override public boolean onPreferenceClick (Preference preference) {// TODO Auto-generated method stub return false ;} so what is the use of these three click events together? Its rules are as follows: after clicking a Preference control, the onPreferenceChange () method will be called back first, that is, whether to save the value (This section describes how to save the value ), then we call back the onPreferenceClick and onPreferenceTreeClick () methods. Therefore, in the onPreferenceClick/onPreferenceTreeClick method, the value of the control we get is the latest value of the Preference control. OnPreferenceClick will then call the callback () method first. If the onPreferenceClick method returns true, the onPreferenceTreeClick () method will not be called. If onPreferenceClick () returns false, the callback () method will () method