"Sail Plan 030" 2015 Sail plan Android Apidemo Devil step App->preferences->preferences from code

Source: Internet
Author: User

Here we use the analogy method, the preferenceactivity and the general activity to make an analogy, can better understand the various classes in the android.preference.

preferenceactivity activity
preference view
preferencegroup viewgroup
preferencescreen linearlayout
checkboxpreference checkbox
edittextpreference edittext
listpreference list
preferencecategory viewgroup
...

The UI of preferenceactivity is defined by preference and so on, like the contentview of activity, the UI of preferenceactivity can be defined by XML, or it can be defined using code. This example is to use code to define the Preferenceactivity UI. Some of the code is as follows:

@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Preferencescreen Root = Getpreferencemanager (). Createpreferencescreen (  This);        Setpreferencescreen (root);    Populatepreferencehierarchy (root); }    Private voidpopulatepreferencehierarchy (Preferencescreen root) {//Inline Preferences        preferencecategory inlineprefcat = new preferencecategory (this); Inlineprefcat.settitle (r.string.inline_preferences); Root.addpreference (INLINEPREFCAT); //Checkbox Preference        checkboxpreferenceCheckboxpref =NewCheckboxpreference ( This); Checkboxpref.setkey ("Checkbox_preference");        Checkboxpref.settitle (r.string.title_checkbox_preference);        Checkboxpref.setsummary (r.string.summary_checkbox_preference);        Inlineprefcat.addpreference (CHECKBOXPREF); //Switch Preference        switchpreferenceSwitchpref =NewSwitchpreference ( This); Switchpref.setkey ("Switch_preference");        Switchpref.settitle (r.string.title_switch_preference);        Switchpref.setsummary (r.string.summary_switch_preference);        Inlineprefcat.addpreference (SWITCHPREF); //Dialog based Preferences        preferencecategoryDialogbasedprefcat =NewPreferencecategory ( This);        Dialogbasedprefcat.settitle (r.string.dialog_based_preferences);        Root.addpreference (DIALOGBASEDPREFCAT); //Edit Text Preference        edittextpreferenceEdittextpref =NewEdittextpreference ( This);        Edittextpref.setdialogtitle (r.string.dialog_title_edittext_preference); Edittextpref.setkey ("Edittext_preference");        Edittextpref.settitle (r.string.title_edittext_preference);        Edittextpref.setsummary (r.string.summary_edittext_preference);        Dialogbasedprefcat.addpreference (EDITTEXTPREF); //List Preference        listpreferenceListpref =NewListpreference ( This);        Listpref.setentries (r.array.entries_list_preference);        Listpref.setentryvalues (r.array.entryvalues_list_preference);        Listpref.setdialogtitle (r.string.dialog_title_list_preference); Listpref.setkey ("List_preference");        Listpref.settitle (r.string.title_list_preference);        Listpref.setsummary (r.string.summary_list_preference);        Dialogbasedprefcat.addpreference (LISTPREF); //Launch Preferences        preferencecategoryLaunchprefcat =NewPreferencecategory ( This);        Launchprefcat.settitle (r.string.launch_preferences);        Root.addpreference (LAUNCHPREFCAT); /** The Preferences Screenpref serves as a screens break (similar to page * broke in word processing). L         IKE for other preference types, we assign * a key where so, it's able to save and restore their instance state. */        //Screen PreferencePreferencescreen screenpref = Getpreferencemanager (). Createpreferencescreen ( This); Screenpref.setkey ("Screen_preference");        Screenpref.settitle (r.string.title_screen_preference);        Screenpref.setsummary (r.string.summary_screen_preference);        Launchprefcat.addpreference (SCREENPREF); /** Can add more preferences to screenpref that'll be shown on the * next screen. childcheckboxpref.setdependency (parent_checkbox_preference);          */        //Example of next screen toggle preferenceCheckboxpreference Nextscreencheckboxpref =NewCheckboxpreference ( This); Nextscreencheckboxpref.setkey ("Next_screen_toggle_preference");        Nextscreencheckboxpref.settitle (r.string.title_next_screen_toggle_preference);        Nextscreencheckboxpref.setsummary (r.string.summary_next_screen_toggle_preference);        Screenpref.addpreference (NEXTSCREENCHECKBOXPREF); //Intent Preference        PreferencescreenIntentpref = Getpreferencemanager (). Createpreferencescreen ( This); Intentpref. setintent (NewIntent (). Setaction (Intent.action_view). SetData (Uri.parse ("Http://www.android.com")));        Intentpref.settitle (r.string.title_intent_preference);        Intentpref.setsummary (r.string.summary_intent_preference);        Launchprefcat.addpreference (INTENTPREF); //Preference AttributesPreferencecategory Prefattrscat =NewPreferencecategory ( This);        Prefattrscat.settitle (r.string.preference_attributes);        Root.addpreference (PREFATTRSCAT); //Visual Parent Toggle PreferenceCheckboxpreference Parentcheckboxpref =NewCheckboxpreference ( This);        Parentcheckboxpref.settitle (r.string.title_parent_preference);        Parentcheckboxpref.setsummary (r.string.summary_parent_preference);        Prefattrscat.addpreference (PARENTCHECKBOXPREF);        Parentcheckboxpref.setkey (parent_checkbox_preference); //Visual Child Toggle Preference//See Res/values/attrs.xml for the <declare-styleable> that defines//Toggleprefattrs.TypedArray A =obtainstyledattributes (r.styleable.toggleprefattrs); Checkboxpreference Childcheckboxpref=NewCheckboxpreference ( This);        Childcheckboxpref.settitle (r.string.title_child_preference);        Childcheckboxpref.setsummary (r.string.summary_child_preference); Childcheckboxpref.setlayoutresource (A.getresourceid (r.styleable.toggleprefattrs_android_preferencelayoutch ILD,0));        Prefattrscat.addpreference (CHILDCHECKBOXPREF);        childcheckboxpref.setdependency (parent_checkbox_preference);    A.recycle (); }

Basically, it corresponds to using XML definition one by one.

Sail Plan 030 2015 sail plan Android Apidemo Devil Step app->preferences->preferences from code

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.