"Android Notes" Quick Development Setup Interface-----preferenceactivity

Source: Internet
Author: User

Usually the app will have a setup interface, as follows:


The usual practice is to define the layout yourself, and then add the response function to the code and save the results to sharedpreferences. Android provides us with a preferenceactivity to simplify the development setup interface. You just have to do this:1. Create a class to inherit preferenceactivity and import the Setup interface layout:

Package Com.example.preferenceactivitydemo1;import Android.os.bundle;import android.preference.PreferenceActivity ;p ublic class Mainactivity extends preferenceactivity{@SuppressWarnings ("deprecation") @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Addpreferencesfromresource (R.xml.main_ preference);}}

2. Add the XML folder under the Res directory and create a file named Main_preference (that is, the resource file referenced in the code).3. Write the resource file. The corresponding resource file for the Setup interface is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><preferencescreen xmlns:android= "http://schemas.android.com/apk/ Res/android "android:title=" title "> <preferencecategory android:summary=" Grouping 1 "android:title=" grouping 1 headings "> <listpreference android:defaultvalue=" @string/defaultvalues "android:dialogtitle=" to The box title "android:entries=" @array/entries "android:entryvalues=" @array/entriesvalue "Android: key= "Mylistpreference" android:summary= "caption description" android:title= "small title"/> <checkboxpreferenc E android:key= "Doubi" android:summaryoff= "tease mode Off" android:summaryon= "tease mode on" a        Ndroid:title= "tease mode" > </CheckBoxPreference> </PreferenceCategory> <preferencecategory            android:summary= "Group 2" android:title= "Group 2 title" > <switchpreference android:key= "Fly"   Android:summaryoff= "Airplane mode is off"         Android:summaryon= "Airplane mode turned on" android:switchtextoff= "Off" android:switchtexton= "on" Android:title= "Airplane mode" > </SwitchPreference> <edittextpreference android:defaultvalue=            "Zhangsan" android:dialogtitle= "edit name" android:key= "name" android:summary= "Edit Your Name" android:title= "edit" > </EditTextPreference> </PreferenceCategory></PreferenceScreen>

it refers to the relevant content of String.xml:


<?xml version= "1.0" encoding= "Utf-8"?><resources> <string    name= "App_name" > preferenceactivitydemo1</string>    <string name= "action_settings" >Settings</string>    <string name= "Hello_world" >hello world!</string><string-array name= "Entries" >    <item> Little Apple </item>    <item> small banana </item>    <item> Small Watermelon </item></string-array>< String-array name= "Entriesvalue" >    <item>apple</item>    <item>banana</item>    <item>watermelon</item></string-array><string name= "Defaultvalues" >apple</ String></resources>

Well, it's so simple ~
All results are saved to the package name _preferences.xml in the data/data/package name/shared_prefs directory, as in Sharepreferences.



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.