Directory (?) [-]
- CheckBox Preference
- XML file
- Storage file of the device
- Composite preference
In the case of Listpreference, a single radio is shown, and if it is multi-selected, checkboxpreferece can be used. We continue with examples of flight information that users choose to display in their flight information (flight, departure time, arrival time, travel time, price).
CheckBox preferencexml File
Add the Checkbox.xml file to the res/xml/as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<preferencescreen xmlns:android= "Http://schemas.android.com/apk/res/android"
android:key= "Flight_columns_pref"
android:title= "Flight Search prefereces"
android:summary= "Set Columns for Search Results" >
<checkboxpreference Android:key= "Show_airline_column_pref"
Android:title= "AirLine"
Android:summaryon= "Show AirLine Column"
Android:summaryoff= "Not Show AirLine Column"/>
<checkboxpreference android:key= "Show_departure_column_pref"
Android:title= "Departure"
android:summary= "Show departure column"/>
<checkboxpreference .../>
<checkboxpreference .../>
<checkboxpreference .../>
</PreferenceScreen>
Android:summaryon and Android:summaryoff represent summary that are displayed separately in the selection and not selection.
Storage file of the device
The contents of the storage preference on the device are as follows:
<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>
<map>
<boolean name= "Show_departure_column_pref" value= "true"/>
<boolean name= "Show_airline_column_pref" value= "false"/>
......
</map>
As seen from the stored XML file, the storage value is Boolean, so Getboolean () is used when reading preference, as follows:
Sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences (this);
Boolean option = Prefs. Getboolean ("Show_airline_column_pref", false);
Showinfo ("Show Airline column:" + option);
Showinfo ("Show departure column:" + prefs. Getboolean("Show_departure_column_pref", false));
Composite preference
Our first example is Listpreference, and the second example is checkpreference, which we want to incorporate into the same XML file as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<preferencescreen xmlns:android= "http ..."
android:title= "Complex Preferences" >
<listpreference android:key= "Selected_flight_sort_option"
android:title= "Flight Options"
android:summary= "Set Search Options"
android:entries= "@array/flight_sort_options"
android:entryvalues= "@array/flight_sort_options_values"
android:dialogtitle= "Choose Flight Options"
Android:defaultvalue= "@string/default_flight_option"/>
<preferencescreen android:key= "Flight_columns_pref"
android:title= "Flight Search prefereces"
android:summary= "Set Columns for Search Results" >
<checkboxpreference android:key= "Show_airline_column_pref"
Android:title= "AirLine"
android:summaryon= "Show AirLine Column"
android:summaryoff= "not Show AirLine Column"/>
<checkboxpreference
android:key= "Show_departure_column_pref"
Android:title= "Departure"
android:summary= "Show departure column"/>
<checkboxpreference .../>
... Other CheckBox preference content ...
</PreferenceScreen>
</PreferenceScreen>
In the same app, Res/xml can set multiple preference files, they can contain the same key preference, actually point to the same value in the storage file, that is, the same preference, just in different preferences Rendered in the UI.
The example code covered in this blog post can be downloaded in the Pro Android Learning: Preference (Preferences) Small example.
RELATED Links: My Android development related articles
"Turn" Pro Android Learning Note (58): Preferences (2): Checkboxpreference