Preferences (2) Preferences interface with Preferencefragment or preferenceactivity

Source: Internet
Author: User

Android application parameters or configuration is managed with sharedpreference storage, but it has no interface, if you want to use the interface,

System in more than 3.0 with Preferencefragment (recommended for it), the following with Preferenceactivity

They have a ListView that displays each configuration item by default, assigning a configuration XML file to them.

The following are examples, which use the same XML.

1. Effects

2. Configure xml:res/xml/my_preference.xml

<?XML version= "1.0" encoding= "Utf-8"?><Preferencescreenxmlns:android= "Http://schemas.android.com/apk/res/android" >    <checkboxpreferenceAndroid:key= "checkbox"Android:title= "Open"Android:summaryon= "Open"Android:summaryoff= "Off"        />        <!--entries is a listpreference list item, entryvalues the value of the specified list item -    <listpreferenceAndroid:key= "List"Android:title= "Please select an option"android:summary= "Please click to select"android:entries= "@array/preference_entries"android:entryvalues= "@array/preference_values"        />    <edittextpreferenceAndroid:key= "text"Android:title= "Please enter"Android:dialogtitle= "Please enter first name"Android:dialogmessage= "No More "android:summary= "Please enter again"/></Preferencescreen>

The list is used. The following is the value of the list item corresponding to each item. Res/values/my_preference.xml

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <String-arrayname= "Preference_entries">        <Item>Java</Item>        <Item>C++</Item>        <Item>Assume</Item>    </String-array>    <String-arrayname= "Preference_values">        <Item>Eclipse</Item>        <Item>Visual Studio</Item>        <Item>notepad++</Item>    </String-array></Resources>

3.preferencefragment.java

ImportAndroid.os.Bundle;Importandroid.preference.CheckBoxPreference;Importandroid.preference.EditTextPreference;Importandroid.preference.ListPreference;Importandroid.preference.PreferenceFragment;ImportAndroid.preference.PreferenceManager;ImportAndroid.widget.Toast; Public classMypreferencefrgmentextendspreferencefragment {Preferencemanager preferncemgr;    Checkboxpreference checkpreference;    Listpreference listpreference;    Edittextpreference editpreference; /** Read each property*/    voidreadpreference () {Toast.maketext (), getactivity (),The currently selected state is: "+checkpreference.ischecked (),0). Show (); Toast.maketext (Getactivity (),"The currently selected list item is:" + listpreference.getentry () + "The value of the currently selected list item is:" + listpreference.getvalue (), 1). Show (); Toast.maketext (Getactivity (),"The currently selected list item is:" + "enter the content as:" + Editpreference.gettext (), 1). Show (); }    voidinit () {Addpreferencesfromresource (r.xml.my_preference);  Key code preferncemgr=Getpreferencemanager (); Checkpreference=(checkboxpreference) preferncemgr. Findpreference ("checkbox");//A checkbox is a android:key defined in XMLListpreference = (listpreference) preferncemgr.findpreference ("list"); Editpreference=(edittextpreference) preferncemgr. Findpreference ("Text"); } @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Init (); }//@Override//Public View Oncreateview (layoutinflater inflater, ViewGroup container,//Bundle savedinstancestate) {//        //Note that if you specify your own layout, the layout must have a ListView//View v = inflater.inflate (R.layout.frgmt_perference, container, false);//return v;//    }}

4.mypreferenceactivity.java

ImportAndroid.os.Bundle;Importandroid.preference.CheckBoxPreference;Importandroid.preference.EditTextPreference;Importandroid.preference.ListPreference;Importandroid.preference.PreferenceActivity;ImportAndroid.preference.PreferenceManager;Importandroid.preference.RingtonePreference;ImportAndroid.widget.Toast; Public classMypreferenceactivityextendspreferenceactivity {Preferencemanager preferncemgr; /** preferenceactivity is mainly used in the following sub-preference*/checkboxpreference checkpreference;    Listpreference listpreference;    Edittextpreference editpreference;    Ringtonepreference RP; /** Read each property*/    Private voidreadpreferences () {Toast.maketext (), Getapplicationcontext (),"The current selected state is:" + checkpreference.ischecked (), 0). Show (); Toast.maketext (Getapplicationcontext (),"The currently selected list item is:" + listpreference.getentry () + "The value of the currently selected list item is:" + listpreference.getvalue (), 1). Show (); Toast.maketext (Getapplicationcontext (),"The currently selected list item is:" + "enter the content as:" + Editpreference.gettext (), 1). Show (); }        Private voidinit () {//Specifies the preferences of the XML file can be, not in the specified layout file, preferenceactivity with the ListView display preferencesAddpreferencesfromresource (r.xml.my_preference); Preferncemgr=Getpreferencemanager (); Checkpreference=(checkboxpreference) preferncemgr. Findpreference ("checkbox");//A checkbox is a android:key defined in XMLListpreference = (listpreference) preferncemgr.findpreference ("list"); Editpreference=(edittextpreference) preferncemgr. Findpreference ("Text"); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Init (); }}

Preferences (2) Preferences interface with Preferencefragment or preferenceactivity

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.