The previous section describes the usage of listpreference. Here we will introduce the usage of several other preferences:
Checkboxpreference:
The result is as follows.CodeSimple:
Activity:
Package cn.com. chenzheng_java.pref; </P> <p> Import android. OS. bundle; <br/> Import android. preference. preferenceactivity; <br/>/** <br/> * @ Description: preferences <br/> * @ author chenzheng_java <br/> * @ since 2011/03/29 <br/> * /<br/> public class mypreferencesactivity extends preferenceactivity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> addpreferencesfromresource (R. XML. checkbox); </P> <p >}< br/>}
Res/XML/checkbox. xml layout File
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <preferencescreen <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Title = "screen title" <br/> Android: key = "mycheckbox_screen" <br/> Android: Summary = "check box Introduction" <br/> </P> <p> <checkboxpreference <br/> Android: key = "Shandong" <br/> Android: Title = "Shandong" <br/> Android: summaryon = "Shandong selected" <br/> Android: summaryoff = "Shandong not selected" <br/> </checkboxpreference> </P> <p> <checkboxpreference <br/> Android: key = "Shanghai" <br/> Android: Title = "Shanghai" <br/> Android: summaryon = "Shanghai selected" <br/> Android: summaryoff = "Shanghai not selected" <br/> </checkboxpreference> </P> <p> <checkboxpreference <br/> Android: key = "Yunnan" <br/> Android: Title = "Yunnan" <br/> Android: summaryon = "Yunnan selected" <br/> Android: summaryoff = "Yunnan unselected" <br/> </checkboxpreference> </P> <p> </preferencescreen> <br/>
The usage is much simpler than listpreference. I will not introduce it here. Here I will show you the XML file in the background:
Cn.com. chenzheng_java.pref_preferences.xml
First of all, you should pay attention to how Android names us, and our package name is cn.com. added chenzheng_java. pref_preferences. In some cases, if we do not inherit preferenceactivity, but use the getsharedpreferences method in activity to perform operations, we will use the file name.
File Content:
<? XML version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> <Br/> <map> <br/> <string name = "mylistpreference"> hebei1 </string> <br/> <Boolean name = "Shanghai" value = "true" /> <br/> <Boolean name = "Shandong" value = "true"/> <br/> </map> <br/>
We can see that it defines a row of data for every checkbox item we select.