Use preferenceactivity to make a standard setup interface

Source: Internet
Author: User

Finally touch a task, do a factory setup, in my opinion, factory settings do not need to be more beautiful, but must be convenient to modify, add features, and then the use of convenience, I thought of the preferenceactivity,android system with the settings is led by it, So the function is powerful, easy to use. In the case of a tablet, you can get a settings without complicated design, the left side is the function list, and the right side is the specific option for a feature.

 Public class extends preferenceactivity{publicvoid onbuildheaders (list target) {         this. Loadheadersfromresource (r.xml.activity_factory_settings, target);}    }

The code section only needs to add a loadheadersfromresource to the onbuildheaders. R.xml.activity_factory_settings This resource file is defined under your code res/xml/

<?XML version= "1.0" encoding= "Utf-8"?><preference-headersxmlns:android= "Http://schemas.android.com/apk/res/android" >    <Headerandroid:fragment= "Com.example.RadioZoneSettings"Android:title= "@string/radio_zone" >        <!--android:summary= "@string/radio_zone_summary" -    </Header>         <Headerandroid:fragment= "com.example." Tvsettings "Android:title= "@string/tv"         >         <!--android:summary= "@string/tv_summary" -    </Header>         <Headerandroid:fragment= "com.example." Logosettings "Android:title= "@string/logo"        >        <!--android:summary= "@string/logo_summary" -    </Header>         <Headerandroid:fragment= "com.example. " Cartypesettings "Android:title= "@string/cartype"         >         <!--android:summary= "@string/cartype_summary" -    </Header>         <Headerandroid:fragment= "com.example. " ApplicationSettings "Android:title= "@string/application"         >         <!--android:summary= "@string/application_summary" -    </Header>         <Headerandroid:fragment= "com.example. " Developmentsettings "Android:title= "@string/development"         >         <!--android:summary= "@string/development_summary" -    </Header> </preference-headers>

Each pair of

 Public classRadiozonesettingsextendslistfragment{protectedArrayadapter<string>Madapter; protectedString mdata[];  Public voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Constructor stub        Super. OnCreate (savedinstancestate); Mdata=getactivity (). Getresources (). Getstringarray (r.array.contries); Madapter=NewArrayadapter<string>(Getactivity (), Android.    R.layout.simple_list_item_single_choice,mdata); }     PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated Method StubView view = Inflater.inflate (R.layout.radio_tv_type,NULL);        Setlistadapter (Madapter); returnview; }     Public voidonactivitycreated (Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate);
Restore the last Set value here
} Public voidOnlistitemclick (ListView L, View V,intPositionLongID) {Super. Onlistitemclick (l, V, position, id); }}

R.array.contries is a string-array I defined in the Res/values/strings.xml.

    <String-arrayname= "Contries">        <Itemname= "Title_china">China</Item>        <Itemname= "Titile_europe">Europe</Item>        <Itemname= "TITILE_USA1">United States</Item>        <Itemname= "TITILE_USA2">Japan</Item>        <Itemname= "Titile_euro1">Russia</Item>    </String-array>

R.layout.radio_tv_type is the entire listfragment layout file, in fact very simple, just a ListView

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >  <ListViewAndroid:id= "@id/android:list"Android:choicemode= "Singlechoice"Android:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "1"Android:divider= "#2f333a"Android:dividerheight= "2DP"Android:drawselectorontop= "false" /></LinearLayout>

Each entry in the ListView is also to define a layout file, and this time I do not define it myself, but instead use the system-provided r.layout.simple_list_item_single_choice.

With these programs you can run. Other practices and its principle is the same, only the option is not required to use the ListView content and options, you need to use other types of fragment, I choose the car label this option to run into this problem, that is, although it is also used in the ListView, but it is not as easy as the example cited here, Need to add a car icon file in front of each item, the difficulty has three, one is so many pictures, how to add, two is no use of adapter meet my custom item, can only write their own adapter. There is how to achieve a single choice, how to manage the state. Next time again.

Use preferenceactivity to make a standard setup 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.