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 us with a series of APIs to help us. Mainly include preferenceactivity, listpreference, edittextpreference, checkboxpreference, ringtonepreference
The following describes the usage of listpreference:
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.
Java code:
/*
Preferences inherits preferenceactivitySo we can conveniently operate on preference.
For example, you can use getpreferencemanager to obtain the preference manager.
So can we not inherit preferenceactivity? Of course you can. In fact, the activity class has a sharedpreferences getsharedpreferences (string name, int mode) method. We can also use it to operate on preference. HoweverIf we do not inherit preferenceactivity, We need to manually Save the data.. Instead of saving data automatically based on your selected items.
So,How is preference automatically saved here? The answer is very simple, that is, the specific implementation of the addpreferencesfromresource method!
*/
Public class mypreferencesactivity extends preferenceactivity {
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Addpreferencesfromresource(R. xml. mylistpreference );
/**
* Getpreferencemanager returns the preference manager object.
*/
Preferencemanager manager = getpreferencemanager ();
// Obtain the preference based on the name (equivalent to ID) specified in Android: Key.
Listpreference = (listpreference) manager.Findpreference("Mylistpreference");
Log. I ("the stored value is", "" + listpreference. getvalue ());
}
}
Res/XML/mylistperference. xml layout File
<? XML version = "1.0" encoding = "UTF-8"?>
<! --
Pay attention to the following points for this file:
First: location,The file is located in RES/XML /..
Second: format,Preferencescreen is the root tag, Listpreference is a sub-tag
Third: Label attribute meaning
Android: unique key identifier, similar to Android: ID, Preferencemanager can use this parameterFindpreferenceObtains the specified preference.
Android: title the title of the entire Screen
Android: Brief description of the summary Option
Android:The text content displayed in the list in the entries pop-up dialog box. Note that an array is specified here.;
Android:Value corresponding to entryvalues and Android: entries
Android:Default Value of defaultvalue when the corresponding value does not exist
Android: dialogtitle information in the pop-up dialog box
-->
<Preferencescreen
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Key = "screen_list"
Android: Title = "title"
Android: Summary = "summary">
<Listpreference
Android: Key ="Mylistpreference"
Android: Title = "title"
Android: Summary = "summary"
Android: entries = "@ array/List_entries"
Android: entryvalues = "@ array/List_entries_value"
Android: dialogtitle = "dialogtitle"
Android: defaultvalue = "@ array/list_entries_value2"
> </Listpreference>
</Preferencescreen>
Res/values/arrays. XML provides initialization data for our list.
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name ="List_entries">
<Item> Shandong </item>
<Item>Fujian</Item>
<Item> Beijing </item>
<Item> Hebei </item>
</String-array>
<String-array name ="List_entries_value">
<Item> shandong1 </item>
<Item>Fujian1</Item>
<Item> beijing1 </item>
<Item> hebei1 </item>
</String-array>
<String-array name = "list_entries_value2">
<Item> shandong2 </item>
<Item> fujian2 </item>
<Item> beijing2 </item>
<Item> hebei2 </item>
</String-array>
</Resources>
When we run andWhen you select FujianYou can view the XML file in shared_prefes as follows:
<? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?>
<Map>
<String name ="Mylistpreference">Fujian1</String>
</Map>