Android Preference API Usage--listpreference (i)

Source: Internet
Author: User

A Listpreference Introduction

We all only know that sharedpreference is very suitable for parameter setting function, here the preference also represents sharedpreference meaning, in sharedpreference, We can quickly save some values into the XML file, and then we can read the settings information to do the corresponding operation.
To simplify application development related to preference, Android provides us with a range of APIs to help us. There are mainly preferenceactivity,checkboxpreference,edittextpreference,listpreference,multiselectlistpreference, Preference,preferencecategory,preferencescreen,ringtonepreference,switchpreference. Using the interface without your own manual data storage, will be automatically based on your selection of data saved, the implementation of the auto-save code in the Addpreferencesfromresource method is specifically implemented, but here can not customize the name of the XML file to save the data. The location of the XML file is stored under the package name/shared_prefs folder that you applied to the data/data/. You can use file Exceplorer to view files in Eclipse.

Two Listpreference effect

1. The interface is displayed as follows (similar to the drop-down box):

2. After clicking, a dialog box will appear, and a list of options can be displayed in the dialog box to achieve a single selection. The dialog box is automatically closed after selection and the values corresponding to the selection are saved to the XML file.

Three Listpreference Usage (Preference preferences)

1. Writing XML files

Precautions:

First: position. The location of the file is under res/xml/.
Second: Format, preferencescreen as root tag, listpreference as child label
Third: label attribute meaning
Android:key Unique identifier, similar to Android:id, Preferencemanager can get the specified preference by findpreference with its parameters.
Android:title the title of the entire screen.
A simple description of the android:summary option.
Android:entries Popup dialog box, the list shows the text content, note that here is an array, Res/values/arrays.xml provides initialization data for our list.
The android:entryvalues corresponds to the android:entries value, as with Android:entries.
Android:defaultvalue the default value when the corresponding value does not exist. The same as android:entries.
Android:dialogtitle the header information in the popup dialog box.

The XML file code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><preferencescreenxmlns:android= "http://schemas.android.com/apk/ Res/android "android:key=" Screen_list "android:title=" title "android:summary=" Description Summary "><listpreferenceandroid:key = "Mylistpreference" android:title= "title" android:summary= "description Summary" android:entries= "@array/list_entries" Android: entryvalues= "@array/list_entries_value" android:dialogtitle= "DialogTitle" android:defaultvalue= "@array/list_ Entries_value2 "></ListPreference></PreferenceScreen>


The Arrays.xml file is as follows:


2. An activity that inherits the Preferenceactivity and implements the Onpreferencechangelistener interface.

Steps:

First: Add an XML file resource. Addpreferencesfromresource (r.xml.mylistpreference);

Second: Getpreferencemanager returns the Preference manager object.

Third: Gets the preference Listpreference object based on the name specified in Android:key (equivalent to the ID).

IV: Add a selection to the Listpreference object to change the listener event.

V: Implement abstract methods in Onpreferencechangelistener. For example, when the data changes, the summary value of listpreference is changed to the value corresponding to the selected item for easy viewing.

The code is as follows:

public class Mypreferencesactivity extends preferenceactivity implements onpreferencechangelistener{@Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Addpreferencesfromresource (r.xml.mylistpreference);/*** Getpreferencemanager returns the preference Manager Object */preferencemanager Manager = Getpreferencemanager ();//Depending on Android: The name specified in key (equivalent to ID) to obtain the preference listpreference listpreference = (listpreference) manager.findpreference ("Mylistpreference"); LOG.I ("Stored value is", "" "+listpreference.getvalue ()); Listpreference.setonpreferencechangelistener (this);} @Overridepublic boolean Onpreferencetreeclick (Preferencescreen preferencescreen,preference Preference) {return Super.onpreferencetreeclick (preferencescreen, preference);} @Overridepublic boolean Onpreferencechange (Preference Preference, Object newvalue) {preference.setsummary (String) NewValue); return true;//returns True to allow change}} 


Four Reference URL

1. http://android.tgbus.com/Android/tutorial/201108/364068.shtml

2. http://www.cnblogs.com/kakafra/archive/2012/09/19/2694444.html

Android Preference API Usage--listpreference (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.