Preferences related to Android (1)

Source: Internet
Author: User

Recently I learned about Android and found that many books have introduced preference preferences. However, most of the books are about how to use preference directly, but they are not mentioned in their usage and origins, I personally despise this practice.

Here, I will give a brief description of it, which may not be rigorous, but at least helps you understand what the preferences are:

Preference is translated by preference. As to what preference is, preference is a convenient data storage tool provided by Android.

Analyze this sentence:

First, we make it clear that preference is related to data storage.

Second, it can help us easily store data! Why must we emphasize the word convenience? The reason is that we can not use it at all. We have n other ways to implement the same function! Its appearance is equivalent to providing us with a convenient tool. Of course, this tool is not necessary.

What scenarios are preference applied in?

This starts with the implementation of preference in Android. In fact, the data stored by preference will be saved in the form of an XML file, and only some data in the basic format can be saved. For example, string/Boolean ....... The XML file is stored in the data/package name of your application/shared_prefs folder.

Various restrictions and implementation mechanisms indicate that preference is very suitable for parameter setting. In fact, it does. By using preference, we can quickly save some values into the XML file, and then we can read these settings for corresponding operations.

To simplify preference-related application development, Android provides a series of APIs to help us. Mainly include preferenceactivity, listpreference, edittextpreference, checkboxpreference, ringtonepreference

The following describes the usage of listpreference:


When we click this option, such a selection page will pop up.

We chose Shandong, and the page will be closed automatically, and the value corresponding to Shandong has been written into the XML file in the background.

Activity Code:

Package chenzheng_java.pref; </P> <p> Import android. OS. bundle; <br/> Import android. preference. listpreference; <br/> Import android. preference. preferenceactivity; <br/> Import android. preference. preferencemanager; <br/> Import android. util. log; <br/>/** <br/> * @ Description: preferences <br/> * @ author chenzheng_java <br/> * @ since 2011/03/29 <br/> * with preferenceactivity inherited, we can easily add preference Row operation. <Br/> * For example, if you can use getpreferencemanager to obtain the preferenceactivity <br/> *, can we not inherit preferenceactivity? Of course you can. Remember, in fact, <br/> * in the activity class has a sharedpreferences getsharedpreferences (string name, int mode) method, we can also operate on preference through it <br/>. Of course, if we do not inherit preferenceactivity, We need to manually <br/> * to save the data. Instead of saving data automatically based on your selected items. <Br/> * How is preference automatically saved here? The answer is very simple, that is, the specific implementation of the addpreferencesfromresource method! <Br/> */<br/> public class mypreferencesactivity extends preferenceactivity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> addpreferencesfromresource (R. XML. mylistpreference); <br/>/** <br/> * getpreferencemanager returns the preference manager object <br/> */<br/> preferencemanager manager = getpreferencemanager (); <br/> // obtain the preference according to the name specified in Android: Key (equivalent to ID) <br/> listpreference = (listpreference) manager. findpreference ("mylistpreference"); <br/> log. I ("the stored value is", "" + listpreference. getvalue (); <br/>}< br/>}

Res/XML/mylistperference. xml layout File

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! -- <Br/> pay attention to the following points for this file <br/> 1: location. The file is located in RES/XML. <Br/> Second: format, preferencescreen is the root tag, listpreference is the sub-tag <br/> third: Tag attribute meaning <br/> Android: unique key identifier, and Android: ID is similar. preferencemanager can use its parameter to obtain the specified preference through findpreference <br/> Android: Title: the title of the entire screen <br/> Android: A brief description of the summary option <br/> Android: entries dialog box, the text content displayed in the list. Note that an array is specified here. <br/> Android: value corresponding to entryvalues and Android: entries <br/> Android: defaultvalue default value when the corresponding value does not exist <br/> Android: title information in the dialog box displayed in dialogtitle <br/> --> <br/> <preferencescreen <br/> xmlns: Android = "" <br/> Android: key = "screen_list" <br/> Android: Title = "title" <br/> Android: summary = "Description abstract" <br/> <listpreference <br/> Android: Key = "mylistpreference" <br/> Android: title = "title" <br/> Android: Summary = "Description abstract" <br/> Android: entries = "@ array/list_entries" <br/> Android: entryvalues = "@ array/list_entries_value" <br/> Android: dialogtitle = "dialogtitle" <br/> Android: defaultvalue = "@ array/list_entries_value2" <br/> </listpreference> </P> <p> </preferencescreen> <br/>

Res/values/arrays. XML provides initialization data for our list.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <string-array name = "list_entries"> <br/> <item> Shandong </item> <br/> <item> fujian </item> <br/> <item> Beijing </item> <br/> <item> Hebei </item> <br/> </string-array> <br/> <string-array name = "list_entries_value"> <br/> <item> shandong1 </item> <br/> <item> fujian1 </item> <br/> <item> beijing1 </item> <br/> <item> hebei1 </item> <br/> </string-array> <br/> <string-array name = "list_entries_value2"> <br/> <item> shandong2 </item> <br/> <item> fujian2 </item> <br/> <item> beijing2 </item> <br/> <item> hebei2 </item> <br/> </string-array> <br/> </resources> <br/>

When we run and select Fujian, we can view the XML file in shared_prefes as follows:

<? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?>
<String name = "mylistpreference"> fujian1 </string>

We can see that mylistpreference is the Android: key value we specified, and fujian1 is a value defined in arrays!

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: 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.