Preferences related to Android preferences (2)

Source: Internet
Author: User

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.

 

Related Article

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.