Preference translation as a preference, Bo friends suggest translating as the preferred item. Some configuration data, some of our last click on the selected content, we hope that the next time the application is still valid, no need to configure or select the user again. Android provides a way to preference this key-value pair to handle this situation by proactively saving the data and taking effect immediately, while Android provides a similar layout for prefernce layouts.
A simple sample of preferenece
Step 1: Write preference XML, add our preference XML file under res/xml/, such as the name C21preference.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<!--preference are organized in a way that has preferencescreen and preferencecategory,preferencecategory are hierarchical organization relationships, in the following sample experience, And Preferencescreen is the most plain and basic way.
<Preferencescreen xmlns:android= "Http://schemas.android.com/apk/res/android">
<!--has four basic builds, this sample learns two. Inside the content comparison chart, very easy to understand, Ringtonepreference is the choice of ring tone, here gives two options, the system default ring tone and silent--
<checkboxpreference android:key= "checkbox"
Android:title= "CheckBox Preference"
Android:summary= "Check it on, check it off "/>
<ringtonepreference android:key= "ringtone"
Android:title= "Ringtone Preference"
android:showdefault= "true"
Android:showsilent= "true"
Android:summary= "Pick a tone, any tone"/>
</PreferenceScreen>
Step 2: Call the XML in Java source code to generate the corresponding preference interface
public class Chapter21test1 extends preferenceactivity {
Span style= "Font-family:microsoft Yahei; Color: #c0c0c0 "> @Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Addpreferencesfromresource (r.xml.c21preference);
}
}
As with layout, the code is very easy. The biggest difference from the previous interface is that when we select the checkbox or select the ringtone, the selection result is retained, and once we exit actitvity, the last option remains, and once again we apply it to the simulator load, The previous option was found to still exist. It is very convenient for us to use preference without having to manually save and read the data.
Step 3: Get the Preference data
Data can be preserved in the preference interface, and we want to be able to read the data so that it can be used in other activity. Suppose there is another activity associated with these data, and you want to be able to synchronize in real time, read the data that preference holds in Onresume (), and then update it accordingly.
First, to obtain the preferences, and then through the key value pair gets the value according to key, the checkbox in the XML key is "checkbox", the value of the type is a Boolean value, and the value of Rintong is a string, such as:
protected void Onresume () {//In another activity
Super.onresume ();
Sharedpreferences prefs =preferencemanager.getdefaultsharedpreferences(this);
System.out.println (The new Boolean (prefs.getboolean("checkbox", false)). ToString ());//false indicates that the return value of the checkbox key is not found.
System.out.println (prefs.getstring("ringtone" ," <unset> "));//<unset> indicates that the return value of the key is not found Ringtong
}
Getting preferences can be done in three ways:
Getpreferences (): ability to acquire preference in the same activity;
Second, getsharedpreferences (): Be able to obtain the level of preferences (), in this example, because we are encapsulated in the same app, can also use sharepreferences prefs = Getsharedpreferences ("Com.wei.android.learning_preferences", 0); The format of the first parameter name is <package_name>_preferences ;
Third, getdefaultsharedpreferences (): Is our example of the way, through the Android Preferences manager to obtain its managed preferences.
Preferenece operation of data
In the example above, the value of preference is preserved and can be read in other activity. If you need to erase the data, you can clear all the preferences by removing () clearing a name of Prefernece,clear (). We can get the editor of Preferences by using edit (), then edit it, and then save the change value by commit ().
Preferenece XML
Preference XML can be organized by preferencecategory. Preferencecategory is able to combine several builds and add headings. We still use the previous XML sample, through preferencecategory to organize, such as the following: Compared with the previous sample, the Chekcbox and Rintong organized into a category, and added "simple Preferences" The title.
<?xml version= "1.0" encoding= "Utf-8"?>
<preferencescreenxmlns:android= "Http://schemas.android.com/apk/res/android">
<preferencecategory android:title= "Simple Preferences" >
<checkboxpreference android:key= "checkbox" ..../>
<ringtonepreference android:key= "Ringtone" .../>
</preferencecategory>
</PreferenceScreen>
Preferencecategory is also able to nest the contents of the Preferencescreen,preferencescreen, which is displayed by a screen. We add a preferencecategory with nested preferencescreen behind the above example. The entire preferencescreen as a component appears, after clicking on a new screen, defined by the into Preferencescreen. Through this relationship, we are able to organize our own preference architecture.
<?xml version= "1.0" encoding= "Utf-8"?> <preferencescreenxmlns:android= "Http://schemas.android.com/apk/res/android"> <preferencecategory android:title= "Simple Preferences" > <checkboxpreference android:key= "checkbox" ..../> <ringtonepreference android:key= "Ringtone" .../> </PreferenceCategory> <preferencecategory android:title= "Detail Screens" > <preferencescreen android:title= "Detail" android:summary= "addtional preference Help in another page" > <checkboxpreferenceandroid:key= "CheckBox2" Android:title= "another Checkbox" Android:summary= "On or OFF, It ' s a problem ..."/> </preferencescreen> </preferencecategory> </PreferenceScreen> |
|
|
In the previous example we used the checkbox and Ringtong, in the following example, we experimented with another two build edittext and list, both of which are the way of the frame. In the above example we continue to add content
<?xml version= "1.0" encoding= "Utf-8"?> <preferencescreen ...> ...... <preferencecategory android:title= "Other Preferences" > <edittextpreference android:key= "text" Android:title= "Text Entry Dialog" Android:summary= "Click to pop up a field for entry" Android:dialogtitle= "Enter Something useful"/> <listpreference android:key= "list" Android:title= "Selection Dialog" Android:summary= "Click to pop yo a list for select" android:entries= "@array/cities" android:entryvalues= "@array/airport_codes" Android:dialogtitle= "Choose a city"/> </PreferenceCategory> </PreferenceScreen>
|
In list by two references @array/cities and @array/airport_codes, we define these two arrays in a resource file. The contents are as follows: <resource> <string-array> <item>Pittsburgh</item> <item>Allentown/Bethlehem</item> <item>Erie</item> ... ... </string-array> <string-array name= "Airport_codes" > <item>PHL</item> <item>PIT</item> <item>ABE</item> ... ... </string-array> </resources>
|
The execution results are as seen in the image on the right. For EditText, the value of the key-value pair is a string, which is what the user enters in the input box. The list is slightly more complicated, and in setting the Listpreference property there is a entires, which is what the list shows to the user, assuming we go to get the value of preference, in this example prefs.getstring ("list" , "<unset>"), then return the corresponding value in Entryvalues, for example, if we select the first Item:pittsburgh, we get a value of PHL. Entries and Entryvalues are one by one corresponding.
RELATED Links: My Android development related articles