Without special requirements, we can use the framework provided by Android to create the system-style preference screen, which can contain preferencecategory and preference<control> Any combination of elements. To create preference screen, you need to add preference controls to the inside, which are: Checkboxpreference, edittextpreference, Listpreference, Multiselectlistpreference and Ringtonepreference, each control contains at least 4 properties: android:key,android:title,android:summary,android: DefaultValue, use a diagram to illustrate it:
There are two ways to create preference, the first of which is to use preferencesactivity. First, create a arrays.xml for storing listpreference under Res/values
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string-array name= "Update_freq_options" >
<item>every minute</item>
<item>5 minutes</item>
<item>10 minutes</item>
<item>15 minutes</item>
<item>every hour</item>
</string-array>
<string-array name= "Magnitude" >
<item>1</item>
<item>3</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
</string-array>
<string-array name= "Magnitude_options" >
<item>1</item>
<item>3</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
</string-array>
<string-array name= "Update_freq_values" >
<item>1</item>
<item>5</item>
<item>10</item>
<item>15</item>
<item>60</item>
</string-array>
</resources>
The Userpreferences.xml is then created under Res/xml to define Preferencescreen
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<preferencescreen
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
<checkboxpreference
android:key= "Pref_antuo_update"
android:title= "Auto Refresh"
Android:summary= "Select to turn on automatic updating"
Android:defaultvalue= "true"/>
<listpreference
android:key= "Pref_update_freq"
android:title= "Prefresh Frequency"
Android:summary= "Frequency at which to refresh earthquake list"
android:entries= "@array/update_freq_options"
android:entryvalues= "@array/update_freq_values"
android:dialogtitle= "Prefresh Frequency"
Android:defaultvalue= "/>"
<listpreference
android:key= "Pref_min_mag"
android:title= "Minmum magnitude"
android:summary= "Select The minimum magnitude earthquake to the"
android:entries= "@array/magnitude_options"
android:entryvalues= "@array/magnitude"
android:dialogtitle= "Magnitude"
Android:defaultvalue= "3"/>
</PreferenceScreen>
Finally, rewrite the OnCreate method in Mainactivity, this mainactivity to inherit preferenceactivity, Add Preferencescreen using Addpreferencesfromresource
Copy Code code as follows:
public class Mainactivity extends preferenceactivity
{
@Override
protected void OnCreate (Bundle savedinstancestate)
{
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Addpreferencesfromresource (r.xml.userpreferences);
}
}
OK, finish, the effect is as shown
The second approach is to use preferencefragment to first create the Usepreferencefragment class to inherit the Preferencefragment class, overriding the OnCreate method, Use Addpreferencesfromresource to load Userpreferences.xml
Copy Code code as follows:
public class Userpreferencefragment extends Preferencefragment
{
@Override
public void OnCreate (Bundle savedinstancestate)
{
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Addpreferencesfromresource (r.xml.userpreferences);
}
}
Then create a preference_headers.xml under Res/xml to associate Usepreferencefragment
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<preference-headers
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
Android:title= "Settings"
android:summary= "Earthquake Refresh Settings"/>
</preference-headers>
Finally, rewrite the Onbuildheaders method in Mainactivity
Copy Code code as follows:
public class Mainactivity extends preferenceactivity
{
@Override
public void Onbuildheaders (list{
TODO auto-generated Method Stub
Loadheadersfromresource (r.xml.preference_headers, target);
}
}
Done, the effect is as follows: then click on the settings will go to the next interface