I believe that everyone is perference familiar, this is what we often say preferences, preference settings, some data can be saved. For example, for some components, we use the last. I hope the next launch will still take effect, annoying. In general this time we will use the Perference key value pair way to handle, before android3.0, we generally go to inherit preference this base class to give the user to present a can set interface. The layout needs to be written by itself, and today we are studying the preference configuration method that uses fragmentation technology after 3.0, which is implemented using Preferencefragement.
--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------
Public abstract class Preferencefragment extends Fragment
Shows the hierarchical relationship of preference objects in a list. These preferences are proactively saved as sharedpreferences. So that users can interact with them.
In order to get another instance of Shaedpreferences, The hierarchy preference in this fragement will use preferencemanager.getdefaultsharedpreferences with one context as the fragement under the same package.
In addition The preferences shown will follow the visual style of the system preferences. It is easy to use an XML file to create a view hierarchy of individual preferences (which can be displayed on many pages). For these reasons, it is recommended to use this fragement (as a superclass) in your application to handle preference issues.
A Preferencescreen object should be at the top of the preference level. In addition, the hierarchy Preferencescreen is then represented by a screen cut-that is, the subsequent preferencescreen should appear on a screen page. Preference Frame processing displays these other screen content from the preference hierarchy.
The preference hierarchy can be formed in very many ways:
A hierarchical structure that is drawn from an XML file.
From different activity, each activity uses Meta-data to make his own preferences in an XML file.
A hierarchy object that is rooted from a preferencescreen.
To get the interface from an XML file, use the Addpreferencefromresource (int) method. The root element should use Preferencescreen. Subsequent elements can point to the subclass of the actual preference item. As mentioned above, subsequent preferencescreen in the hierarchy will result in a screen cutting place.
To specify an intent to query activitiy with their preferences, use the Addpreferencefromintent method.
Each activity can specify Meta-data in the manifest file to point to an XML file resource. These resource files will be populated in a separate preference hierarchy and shown through this fragment.
In order to specify an object with Preferencescreen as the root element. Use the Setpreferencescreen (Preferencescreen) method.
Convenience. This fragment implements a click event listener for discretionary preferences in the current hierarchy. Onpreferencetreeclick (preferencescreen,preference).
The above translation from the official documents of the preferencefragment, can be self-access to their original instructions
--------------------------------------------------------------------------------------------------------------- -------------------------
According to the official documentation and samples from the Apidemo, I wrote a sample myself using Preferencefragment, with the effect almost identical to the previous use of inheritance preferenceactivity, such as the following:
① Create a project
② Add menu button in mainacitivity and corresponding effect
Package Com.example.fragementexam;import Android.os.bundle;import Android.view.menu;import Android.view.MenuItem; Import Android.app.activity;import Android.content.intent;public class Mainactivity extends Activity {private static final int menu_setting = 1; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//TODO auto-generated method Stubmenu.add (0,menu_setting,1, " Settings "). SetIcon (Android. r.drawable.ic_menu_preferences); return Super.oncreateoptionsmenu (menu);} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//TODO auto-generated method stubsuper.onoptionsitemselected (item); Intent Intent = new Intent (this, fragmentpreferences.class); StartActivity ( Intent); return false;}}
③ Create a new XML directory below the Res directory and create a new Preferences.xml file inside
<preferencescreen xmlns:android= "Http://schemas.android.com/apk/res/android" > <preferencecategory android:title= "In-line preferences" > <checkboxpreference android:key= "checkbox_preference" Android:summary= "This is a check box" android:title= "check box settings"/> </PreferenceCategory> <preferencecate Gory android:title= "dialog-based preferences" > <edittextpreference android:dialogtitle= "Please enter your favorite kind Family "android:key=" Edittext_preference "android:summary=" a sample "android:title=" Using the Edit Text dialog box, enter Your favorite race "/> <listpreference android:dialogtitle=" Please select a "android:entries=" @array/entries _list_preference "android:entryvalues=" @array/entryvalues_list_preference "android:key=" List_prefere NC "android:summary=" a sample using the list dialog box "android:title=" Please select an item "/> </PreferenceCategory> &L T Preferencecategory android:title="Launch Preferences" > <!--this preferencescreen tag serves as a screens break (similar-page break in WOR D processing). Like for other preference types, we assign a key here so it's able to save and restore their instance state. --<preferencescreen android:key= "Screen_preference" android:summary= "show there is also a preference configuration page" Android:title= "page Preferences" > <!--you can place many other preference items here, which will be rendered on the next page-<checkboxpre Ference android:key= "Next_screen_checkbox_preference" android:summary= "There is also a page to show but for the same level of preference Configure the Android:title= check box settings/> </PreferenceScreen> <preferencescreen and Roid:summary= "Start an activity from one intention" android:title= "intent Preferences" > <intent android:actio N= "Android.intent.action.VIEW" android:data= "http://www.baidu.com"/> </PreferenceScreen> </preferencecategory> <preferencecategory android:title= "Preference attributes" > <checkboxpreference Andro id:key= "Parent_checkbox_preference" android:summary= "This is a visible parent class" android:title= "Parent class check box preferences"/> <!--the visible type of a subclass is defined by the style attribute---<checkboxpreference android:dependency= "Parent_checkbox_preference" android:key= "Child_checkbox_preference" android:layout= "? Android:attr/preferencelayoutchild" Android:summary= "This is a visible subclass" Android:title= "Sub-class checkbox preferences"/> </preferencecategory></preferencesc Reen>
④fragmentpreferences.java part of the code
Package Com.example.fragementexam;import Android.app.activity;import Android.os.bundle;import Android.preference.preferencefragment;public class Fragmentpreferences extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Getfragmentmanager (). BeginTransaction (). Replace (Android. R.id.content, New Prefsfragement ()). commit ();} public static class Prefsfragement extends preferencefragment{@Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Addpreferencesfromresource (R.xml.preferences );}}}
Finally, the effect after execution:
About learning to use it in Preferencefragment is so, later in the higher version number than developing such a way when considered.
Android Fragement Learning Note (iii)----preferencefragment use