"Turn" Pro Android Learning Note (58): Preferences (2): Checkboxpreference

Source: Internet
Author: User

Directory (?) [-]

    1. CheckBox Preference
      1. XML file
      2. Storage file of the device
    2. 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

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: info-contact@alibabacloud.com 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.