PreferenceScreen application, preferencescreen

Source: Internet
Author: User

PreferenceScreen application, preferencescreen
PreferenceScreen preference is preferred. PreferenceScreen translates PreferenceScreen into "preference display", which means you can flexibly define the display content style based on the characteristics, A screen can contain multiple styles, such as the built-in sound setting interface.
 
To achieve this display effect, you only need to use the PreferenceScreen class. Create an xml folder in the project resource file, and create a preferences. xml file in it.
If the root element is PreferenceScreen, the entire screen is displayed. The PreferenceCategory label is nested in the PreferenceCategory label, indicating the preference category. In the PreferenceCategory label, check boxes, input boxes, lists, and other display. the available controls are available on android. for more information about preference packages, see. after the xml file is compiled, it needs to be loaded into the activity. For xml loading of preference display, you can use addPreferencesFromResource () in PreferenceActivity. Therefore, the Activity must inherit PreferenceActivity. since the displayed screen contains the check box and the control in the input box, we must be interested in the selection or not and the content in the input box. How can we get the screen content?
The onPreferenceTreeClick method in the rewrite activity triggers this method when you perform operations on the content displayed on the screen.
Java code

  1. SharedPreferences contentPreference = preference. getSharedPreferences ();
  2. Boolean checkbox_toggle = contentPreference. getBoolean ("checkbox_preference", false );
  3. String animalName = contentPreference. getString ("edittext_preference", "default ");

You can get the input content on the screen, which is obtained in the form of key-value like SharedPreference object, where key is the value corresponding to the key attribute in the xml control label.
Display Effect:
 
Corresponding xml file:
Java code
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <PreferenceScreen
  3. Xmlns: android = "http://schemas.android.com/apk/res/android">
  4. <PreferenceCategory
  5. Android: title = "show preferences in a row">
  6. <CheckBoxPreference
  7. Android: key = "checkbox_preference"
  8. Android: title = "Switch Preferences"
  9. Android: summary = "this is a switch button"/>
  10. </PreferenceCategory>
  11. <PreferenceCategory
  12. Android: title = "dialog box-based preferences">
  13. <EditTextPreference
  14. Android: key = "edittext_preference"
  15. Android: title = "text input Preferences"
  16. Android: summary = "Use a text box dialog box"
  17. Android: dialogTitle = "Enter your pet"/>
  18. <ListPreference
  19. Android: key = "list_preference"
  20. Android: title = "list Preferences"
  21. Android: summary = "Use a List dialog box"
  22. Android: entries = "@ array/entries_list_preference"
  23. Android: entryValues = "@ array/entryvalues_list_preference"
  24. Android: dialogTitle = "select one"/>
  25. </PreferenceCategory>
  26. <PreferenceCategory
  27. Android: title = "Startup Preferences">
  28. <PreferenceScreen
  29. Android: key = "screen_preference"
  30. Android: title = "screen"
  31. Android: summary = "show another preference screen">
  32. <! -- You can place more preferences here that will be shown on the next screen. -->
  33. <CheckBoxPreference
  34. Android: key = "next_screen_checkbox_preference"
  35. Android: title = "Switch Preferences"
  36. Android: summary = "preferences on another screen"/>
  37. </PreferenceScreen>
  38. <PreferenceScreen
  39. Android: title = "intention Preferences"
  40. Android: summary = "starting an Activity with intent">
  41. <Intent android: action = "android. intent. action. VIEW"
  42. Android: data = "http://www.android.com"/>
  43. </PreferenceScreen>
  44. </PreferenceCategory>
  45. <PreferenceCategory
  46. Android: title = "Preference property">
  47. <CheckBoxPreference
  48. Android: key = "parent_checkbox_preference"
  49. Android: title = "parent switch"
  50. Android: summary = "this is a parent switch"/>
  51. <CheckBoxPreference
  52. Android: key = "child_checkbox_preference"
  53. Android: dependency = "parent_checkbox_preference"
  54. Android: layout = "? Android: attr/preferenceLayoutChild"
  55. Android: title = "sub-switch"
  56. Android: summary = "this is a sub-switch"/>
  57. </PreferenceCategory>
  58. </PreferenceScreen>

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.