Preferenceactivity for Android

Source: Internet
Author: User

I have not introduced preferenceactivity in many books, but I just used it in the project. So I will summarize my usage here, which is also convenient for future search.

Perferenceactivity:

Android system (left) musicplayer setting (right)

Well, we can see that the Android system uses preferenceactivity to configure and manage system information. How does it save data and create prefenceactivity, the more important thing is how to trigger the corresponding event.

First, let's look at how preferenceactivity stores data. We all know about the Android system. The Android system has four basic data storage methods: sharedpreference, file, SQLite, and contentprovider. As you can see, preference is saved as a key-Value Pair using sharedpreferneces. Of course, we can also use sharedpreferences to obtain the value set by preferenceactivity.

 

The second question: how to create a preferenceactivity. In fact, eclipse provides corresponding creation tools, which are basically the same as creating layout. The procedure is as follows:

Create an android project and add an android XML file. Note: This time we chose not layout, but preference, and note that the folder path is res/XML.

After adding the preference. xml file, open it in RES/XML. Android also provides two editing modes: visual structure design and XML source code design. We recommend that you use structure to create a cluster. :

Next, let's take a look at which elements are available for prefeneceactivity. Click the Add button. The following items are displayed in the new window:

Checkboxpreference: Specifies the checkbox option, which is the true or flase of the corresponding value.

Edittextpreference: Enter the edit box. The value is of the string type. A dialog box is displayed for you to enter.

 

Listpreference: select a list. In the displayed dialog box, select a list.

 

Preference: displays text only and must be combined with others.

Preferencecategory: used for grouping. The effect is as follows:

Preferencescreen: root element of preferenceactivity, which must be.

 

Ringtonepreference: Select system sound.

OK. The basic elements of preferenc are described. The next section uses them to create and display a complete preference.

-------------------------------------------------------------------------------

The first part of the analysis of musicplayer setting is "My location", which includes "using a wireless network cable" and "using GPS", both of which are checkbox. Learn from the previous section, it should include a preferencecategory and two checkboxpreference.

XMLCode:

 
<Preferencecategory Android: Title ="My location"Android: Key ="Set_local"> <Checkboxpreference Android: Key ="Apply_wifi"Android: Summary ="Use wireless networks in applicationsProgram(For example, Google map)"
 
Android: Title ="Use Wireless Networks"Android: defaultvalue =" true "> </checkboxpreference> <checkboxpreference Android: Key ="Apply_gps"Android: Summary ="Locate the street level (requires more power consumption and the weather permits)"
Android: Title ="Use GPS"> </Checkboxpreference> </preferencecategory>

The above code can also be directly generated using the IDE tool provided by Android. The video structure is as follows:

Preferencecategory attribute analysis:

Title: The title displayed.

Key: the unique identifier (at least in the same program). sharedpreferences also saves data using this key value, or obtains the saved information using the key value (the same as below ).

Checkboxpreference attribute analysis:

Key: unique identifier.

Title: Display title (large font display)

Summary: subtitle (small font display)

Defaultvalue: Default Value (of course, it can only be true or false)

 

The second part of preference. XML is "wireless and network settings". This part contains a lot of content and is a little complicated. It is analyzed step by step.

XML code:

 <  Preferencecategory   Android : Title = "Wireless and network settings"  >  <  Checkboxpreference   Android : Key = "Apply_fly"  Android : Summary ="Disable all wireless connections"   Android : Title = "Flight mode"  >  </  Checkboxpreference  >  <  Checkboxpreference   Android : Key = "Apply_internet"  Android : Summary ="Disable shared Internet connection via USB" 
    Android : Title = "Internet sharing"  >  </  Checkboxpreference  >  <  Checkboxpreference   Android : Key = "Apply_wifi"  Android :Summary = "Enable Wi-Fi"   Android : Title = "Wi-Fi"  >  </  Checkboxpreference  >  <  Preference   Android : Summary = "Set and manage wireless access points"   Android :Title = "Wi-Fi Settings"  Android : Dependency = "Apply_wifi"   Android : Key = "Wifi_setting"  >  </  Preference  >  <  Checkboxpreference   Android :Key = "Apply_bluetooth"  Android : Summary = "Enable Bluetooth"   Android : Title = "Bluetooth"  >  </  Checkboxpreference  >  <  Preference   Android :Summary = "Manage connections, device names, and testability"  Android : Title = "Bluetooth Settings"   Android : Dependency = "Apply_bluetooth"  Android : Key = "Maid"  >  </  Preference  > <  Edittextpreference   Android : Key = "Number_edit"  Android : Title = "Enter the phone number"  >  </  Edittextpreference  >  <  Listpreference   Android :Title = "Department Settings"   Android : Entries = "@ Array/Department"  Android : Entryvalues = "@ Array/department_value"  
  Android :  dialogtitle  = " select a department "  Android :  key  = " depart_value " >    listpreference  >   ringtonepreference   Android :  ringtonetype  = " all "  Android :  title  = " Linglong " 
Android:Showdefault="True" Android:Key="Ring_key"
 
 Android:Showsilent="True"></Ringtonepreference></Preferencecategory>

Structure diagram:

In the second part, the first three are checkboxpreference.<Preference Android:Key=Start with "Export th_setting"/>.

Preference attribute analysis:

Key: unique identifier.

Title: Display title (large font display)

Summary: subtitle (small font display)

Dependency: attach (meaning), which indicates that this element is attached to a certain element (usually checkboxpreference), and the dependency value is the key of the affiliated element. The preference element in the above Code is attached to the checkpreference element whose key is equal to "apply_bluetooth". When the checkpreference value is true, the preference is available; otherwise, it is unavailable.

 

Edittextpreperence attribute analysis:

Key: unique identifier.

Title: Display title (large font display)

Listpreference attribute analysis:

Key: unique identifier.

Title: Display title (large font display)

Dialogtitle: the title of the pop-up dialog box.

 Entries: The value displayed in the list. It is an array and can be read through resource files.

Entryvalues: The value actually saved in the list, which also corresponds to entries. It is an array and can be read through resource files. The following code displays the content in the arrays. xml file:

 <  Resources  >  <  String - Array   Name ="Department"  >  <  Item  > General Department </  Item  >  <  Item  > Administration Department </  Item  >  <  Item  > Foreign Trade Department</  Item  >  </  String -Array >  <  String - Array   Name = "Department_value"  >  <  Item  > 001 </ Item  >  <  Item  > 002 </  Item  >  <  Item  > 003 </  Item  >  </  String -Array > </  Resources  > 

Ringtonepreference: Ling Sheng (not used for the moment), skipped for the moment.

OK, the preference. XML content has been analyzed, and the attributes are roughly the same. I believe that this is the case when you try it automatically. So how can we display the content in preference. xml?

Layout is displayed by continuing from the activity class. previously mentioned, preferenceactivity is used to display preference, so you only need to create a class inherited from preferenceactivity. The Code is as follows:

 
Public class setting extends preferenceactivity {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // The value is automatically saved to sharepreferencesaddpreferencesfromresource (R. XML. preference );}}

 

The next step is to run the program and display the labor results. At this point, most of the work has been completed, and all the values will be saved to sharedpreferences. We can also read the saved results.

Another important question is how to respond to preferenceactivity operations. In fact, you only need to override the onpreferencetreeclick method of preferenceactivity. You can use the preference parameter to determine the element and perform operations as needed.

 
@ Override
 
Public BooleanOnpreferencetreeclick (preferencescreen, preference ){Return False;}

 

OK. The content about preferenceactvity will be completed, and any major discoveries will be completed in the future.

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.