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:
- Package EOE. Demo;
- Import Android. OS. Bundle;
- Import Android. Preference. listpreference;
- Import Android. Preference. preferenceactivity;
- Import Android. Preference. preferencemanager;
- Import Android. util. log;
- /**
- * @ Description: preferences
- * @ Author chenzheng_java
- * @ Since 2011/03/29
- * Inherits preferenceactivity, which allows you to conveniently operate on preference.
- * For example, you can use getpreferencemanager to obtain the preference manager.
- * Can we not inherit preferenceactivity? Of course you can. Remember, in fact, in the activity class
- * There is a sharedpreferences getsharedpreferences (string name, int mode) method.
- * You can also perform preference operations. Of course, if we do not inherit preferenceactivity, We need to manually
- * The data is saved. Instead of saving data automatically based on your selected items.
- * Then, how does preference automatically save it? 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 ());
- }
- }
Copy code
Res/XML/mylistperference. xml layout File
Java code:
- <? 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, and listpreference is the sub-tag.
- Third: Label attribute meaning
- Android: Unique Key Identifier. Similar to Android: ID, preferencemanager can use it as a parameter to obtain the specified preference through findpreference.
- Android: title the title of the entire Screen
- Android: a brief description of the summary Option
- Android: The text content displayed in the list in the entries pop-up dialog box. Note that the content is an array.
- Android: 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>
Copy code
Res/values/arrays. XML provides initialization data for our list.
Java code:
- <? 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>
Copy code
When we run and select Fujian, we can view the XML file in shared_prefes as follows:
Java code:
- <? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?>
- <Map>
- <String name = "mylistpreference"> fujian1 </string>
- </Map>
Copy code