"Sail Plan 027" 2015 Sail plan Android Apidemo Devil Step app->preferences->preferences from XML Preferences interface

Source: Internet
Author: User

We have in the previous example Android Apidemo example parsing (9): App->activity->persistent state describes the use of shared preferences to store some states, shared Preferences more general usage is to store some application preferences (settings).

Package Android.preference provides many classes that make it easy for applications to display and set application-related preferences. Of course you can use a custom UI to configure these program preferences. But using classes defined in Android.preference can give the user a unified UI (and the settings of Android itself).

Typically, the program prefers to use a separate activity (derived from preferenceactivity) to complete. In Preferenceactivity,Preferencescreen is the root element of layout, which can contain other such as:checkboxpreference, Edittextpreference, Listpreference, preferencecategory or ringtonpreference. When using preference, all program preferences are automatically saved in the application's sharedpreferences, and the app can access these preferences through Getsharedpreferences ().

    • Checkboxpreference uses a checkbox to display a configuration item.
    • Edittextpreference uses a text box to display a configuration item that allows the user to enter text. Eject Dialog
    • Listpreference uses a set of radio button (lists) from which to select an item. Eject Dialog
    • Multiselectlistpreference uses a set of checkboxes that allow the configuration item to have multiple values.
    • Ringtonpreference allows the user to select a ringtone from

The base class for these classes is Preference , which is a base class, or a custom Preference can be defined.

    • Preferencegroup can define a group for multiple preference, preferencecategory, Preferencescreen for its subclasses.
    • Preferencecategory can also contain multiple preferneces, which can provide a caption if the group is disable.
    • Preferencescreen is the root element of the preferences hierarchy, Preferencescreen can be nested. The inner layer of the Preferencescreen will be displayed with a new screen, a bit similar to the paging feature in Word.

Preferences from XML describes the use of XML to define application preferences and uses preferenceactivity to display this preference.

R.xml.preferences is defined as follows:

<!--This is a primitive example showing the different types of preferences available. -->< preferencescreen xmlns:android= "Http://schemas.android.com/apk/res/android" > < preferencecategoryandroid:title             = "@string/inline_preferences"> < checkboxpreference android:key= "Checkbox_preference"Android:title= "@string/title_checkbox_preference"android:summary= "@string/summary_checkbox_preference"/> </PreferenceCategory> <preferencecategory Android:title= "@string/dialog_based_preferences" > < edittextpreference android:key= "Edittext_preference"Android:title= "@string/title_edittext_preference"android:summary= "@string/summary_edittext_preference" Android:dialogtitle= "@string/dialog_title_edittext_preference"/> < listpreference android:key= "List_preference"Android:title= "@string/title_list_preference"android:summary= "@string/summary_list_preference"android:entries= "@array/entries_list_preference"android:entryvalues= "@array/entryvalues_list_preference"Android:dialogtitle= "@string/dialog_title_list_preference"/> </PreferenceCategory> <preferencecategory Android:title= "@string/launch_preferences" > <!--this preferencescreen tag serves as a screen Break(Similar to page BreakIn word processing). like forOther preference types, we assign a key here so it's able to save and restore their instance state.-< preferencescreen android:key= "Screen_preference"Android:title= "@string/title_screen_preference"android:summary= "@string/summary_screen_preference" > <!--You can place more preferences here that'll be s Hown on the next screen. -<checkboxpreference Android:key= "Next_screen_checkbox_preference"Android:title= "@string/title_next_screen_toggle_preference"android:summary= "@string/summary_next_screen_toggle_preference"/> </PreferenceScreen> <Preferencescreen Android:title= "@string/title_intent_preference"android:summary= "@string/summary_intent_preference" > <Intent android:action= "Android.intent.action.VIEW" android:data= "http://www.android.com" 
    /> </PreferenceScreen> </PreferenceCategory> <preferencecategory Android:title= "@string/preference_attributes" > <checkboxpreference Android:key= "Parent_checkbox_preference"Android:title= "@string/title_parent_preference"android:summary= "@string/summary_parent_preference"/> <!--the visual style of a child was defined by ThisStyled Theme attribute. -<checkboxpreference Android:key= "Child_checkbox_preference" android:dependency= "Parent_checkbox_preference" android:layout= "Android Oid:attr/preferencelayoutchild "Android:title= "@string/title_child_preference"android:summary= "@string/summary_child_preference"/> </PreferenceCategory> </PreferenceScreen>

Using addpreferencesfromresource (r.xml.preferences) in your code shows the preferences defined by the XML.

The root element in XML is Preferencescreen.

Checkboxpreference

Preferencecategory defines the title of the group configuration, Checkboxpreference uses a checkbox to display the configuration item.

    <preferencecategory            android:title= "@string/inline_preferences" >                    <  Checkboxpreference                Android:key= "Checkbox_preference"                android:title= "@string/title_ Checkbox_preference "                android:summary=" @string/summary_checkbox_preference "/>    </ Preferencecategory>

Edittextpreference

Edittextprefernece Displays a text box to accept user input:

        <edittextpreference                android:key= "Edittext_preference"                android:title= "@string/ Title_edittext_preference "                android:summary=" @string/summary_edittext_preference "                android: DialogTitle= "@string/dialog_title_edittext_preference"/>

Listpreference

Displays a set of radio button.

        <listpreference                android:key= "List_preference"                android:title= "@string/title_list_ Preference "                android:summary=" @string/summary_list_preference "                android:entries=" @ Array/entries_list_preference "                android:entryvalues=" @array/entryvalues_list_preference "                 android:dialogtitle= "@string/dialog_title_list_preference"/>

Preferencescreen

Use the new screen to display the application preference configuration.

<!--this preferencescreen tag serves as a screen Break(Similar to page BreakIn word processing). like forOther preference types, we assign a key here so it's able to save and restore their instance state.-<Preferencescreen Android:key= "Screen_preference"Android:title= "@string/title_screen_preference"android:summary= "@string/summary_screen_preference" > <!--You can place more preferences here that'll be s Hown on the next screen. -<checkboxpreference Android:key= "Next_screen_checkbox_preference"Android:title= "@string/title_next_screen_toggle_preference"android:summary= "@string/summary_next_screen_toggle_preference"/> </PreferenceScreen>

Intent Preference

In addition to a new screen, Preferencescreen can also be used to start an activity, the following definition launches the browser to open http://www.android.com.

        <preferencescreen                android:title= "@string/title_intent_preference"                android:summary = "@string/summary_intent_preference" >            <intent android:action= "Android.intent.action.VIEW"                     Android:data= "http://www.android.com"/>        </PreferenceScreen>

Dependence between the preference

The last example indicates that a dependency between preference can be defined. The child preferences is only enable when the parent preference is selected.

<preferencecategory Android:title= "@string/preference_attributes" > <checkboxpreference Android:key= "Parent_checkbox_preference"Android:title= "@string/title_parent_preference"android:summary= "@string/summary_parent_preference"/> <!--the visual style of a child was defined by ThisStyled Theme attribute. -<checkboxpreference Android:key= "Child_checkbox_preference"android:dependency= "Parent_checkbox_preference"Android:layout= "? Android:attr/preferencelayoutchild"Android:title= "@string/title_child_preference"android:summary= "@string/summary_child_preference"/> </PreferenceCategory>

"Sail Plan 027" 2015 Sail plan Android Apidemo Devil Step app->preferences->preferences from XML Preferences interface

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.