PreferenceActivity (PreferenceActivity page)

Posted by ravravid on Mon, 20 May 2019 20:30:36 +0200

Introduction to Preference Activity

It has always been thought that the user interface of the software was written by the author himself. Today, I found that a ready-made Preference Activity can be used, which is very convenient. You should all know that most applications have options pages. Most of the options pages referred to here have been inherited from Preference Activity (of course, you can write them yourself if you're pushed). If you don't know what Preference is, please move on.( http://blog.csdn.net/plussoft/article/details/9152573 ) Like other preference settings, the information generated by PreferenceActivity is stored in XML. It is also worth noting that the way of naming the XML file generated by Preference Activity is dead. The generated XML file is named "Project Name > Preference. xml" under data/data/<Project Name>/shared_prefs/, which cannot be changed. Formally, because of this, the XML generated by Preference Activity is also known as global settings and application settings, and has the Supreme position. In Preference Activity, all modifications update the XML file automatically, without requiring us to set up a large number of listeners manually. In addition, we need to review various ways to obtain Shared Preferences:

getPreferences(): You can get preference s in the current Activity, which I have not used (mainly not understood);

2. PreferenceManager. getDefaultShared Preferences (): As the name implies, the Preferences generated by PreferenceActivity are references to obtain global variables;

3. getSharedPreferences(): You can get all the preferences in the application, that is, in the( http://blog.csdn.net/plussoft/article/details/9152573 ) As mentioned. The first two methods are based on this method.

Back to Preference Activity. To simplify preference-related application development, android provides us with a series of APIs to help us. In Preference Activity, we are provided with four options integration controls: List Preference, EditText Preference, CheckBox Preference and Ringtone Preference. If it's not enough for you, you have to develop it by yourself. In addition, Ringtone Preference is not introduced here. It feels really useless to me personally - what applications will use your ringtone? Let's start by showing the unique charm of Preference Activity.


List Preference

Let's first look at the final effect.


The function is not elaborated much. Take a look at the MainActivity code:

  1. package com.plusjun.preference;  
  2.   
  3. import android.os.Bundle;  
  4. import android.preference.PreferenceActivity;  
  5.   
  6. /* 
  7.  * Remember to inherit PreferenceActivity instead of Activity 
  8.  */  
  9. public class MainActivity extends PreferenceActivity  {  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         /* Put in the settings page file 
  14.          * Here, R.xml.mylistperference can be seen as a layout file 
  15.          */  
  16.         /* 
  17.          * addPreferencesFromResource The method was discarded in Android 3.0 
  18.          * But there is no alternative. 
  19.          * Google's idea is that we want to inherit Preference Fragment to implement the default options page. 
  20.          * But it's too difficult. Really? 
  21.          */  
  22.         addPreferencesFromResource(R.xml.mylistperference);  
  23.     }  
  24. }  

res/xml/mylistperference.xml layout file

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--   
  3. The following points should be noted for this document.
  4. First: location. The location of this file is under res/xml/.   
  5. Second: Format, Preference Screen is the root tag, List Preference is the subtag. Preference Screen can be seen as a large framework.
  6. Third: the meaning of label attributes
  7. android:key unique identifier, similar to android:id, which Preference Manager can use as a parameter to obtain the specified preference through find Preference
  8. Note that the value of this android:key is also the XML "key" name in the Preference file.
  9. android:title headline
  10. android:summary: The small word under the heading (this is available as a tab)
  11. In the android:entries pop-up dialog box, the list shows the text content. Note that an array is specified here.
  12. android:entryValues corresponding to android:entries
  13. android:defaultValue
  14. Android: The title information in the dialogTitle pop-up dialog box
  15. More label settings await your discovery.
  16.  -->  
  17. <PreferenceScreen  
  18.   xmlns:android="http://schemas.android.com/apk/res/android"  
  19.   android:key="screen_list"  
  20. android:title= "title"
  21. android:summary= "summary of instructions"
  22.   >  
  23.   <ListPreference  
  24.     android:key="myListPreference"  
  25. android:title= "title"
  26. android:summary= "summary of instructions"
  27.     android:entries="@array/list_entries"  
  28.     android:entryValues="@array/list_entries_value"  
  29. android:dialogTitle= "Here is the pop-up box"
  30.     android:defaultValue="@string/deflault_list_entries_value"  
  31.   ></ListPreference>  
  32.     
  33. </PreferenceScreen>  

Our data source is in the strings.xml file, which is a good habit. Of course, strings.xml file is not necessary. You can customize the file, but you can't have the same label.

Add a code snippet to strings.xml:

  1. <string-array name="list_entries_value">  
  2.     <item>shandong</item>  
  3.     <item>fujian</item>  
  4.     <item>beijing</item>  
  5.     <item>hebei</item>  
  6. </string-array>  
  7.   
  8. <string-array name="list_entries">  
  9.     <item>Shandong</item>  
  10.     <item>Fujian</item>  
  11.     <item>Beijing</item>  
  12.     <item>Hebei</item>  
  13. </string-array>  
  14.   
  15. <string name="deflault_list_entries_value">beijing</string>  

Under data/data/<project name>/shared_prefs/, the name is "<project name>_Preference.xml", which reads as follows:

  1. <?xml version='1.0' encoding='utf-8' standalone='yes' ?>  
  2. <map>  
  3. <string name="myListPreference">fujian1</string>  
  4. </map>  

Thus, android:key="myListPreference" is the key name in XML, and the value is the value we choose.


III. CheckBox Preference

The results are as follows:


We continue to take a project to modify. MainActivity remains unchanged:

  1. package com.plusjun.preference;  
  2.   
  3. import android.os.Bundle;  
  4. import android.preference.PreferenceActivity;  
  5.   
  6. public class MainActivity extends PreferenceActivity  {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         addPreferencesFromResource(R.xml.mylistperference);  
  11.     }  
  12. }  

res/xml/checkbox.xml layout file

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:key="screen_list"  
  4.     android:summary="Explanatory summary"  
  5.     android:title="Title" >  
  6.   
  7.     <CheckBoxPreference  
  8.         android:key="medf"  
  9.         android:summaryOff="Maldives not selected"  
  10.         android:summaryOn="Maldives was selected"  
  11.         android:title="Maldives" >  
  12.     </CheckBoxPreference>  
  13.     <CheckBoxPreference  
  14.         android:key="mlqs"  
  15.         android:summaryOff="Mauritius was not selected"  
  16.         android:summaryOn="Mauritius was selected"  
  17.         android:title="Mauritius" >  
  18.     </CheckBoxPreference>  
  19.   
  20. </PreferenceScreen>  

Finally, the file content generated in "<Project Name>_Preference.xml":

  1. <?xml version='1.0' encoding='utf-8' standalone='yes' ?>  
  2. <map>  
  3. <boolean name="medf" value="true" />  
  4. <boolean name="mlqs" value="false" />  
  5. </map>  

Clearly, CheckBox Preference is much simpler than List Preference.


IV. EditText Preference

The results are as follows:


When we click on the input name of the main page, the dialog box pops up and lets us enter text.

MainActivity remains unchanged:

  1. package com.plusjun.preference;  
  2.   
  3. import android.os.Bundle;  
  4. import android.preference.PreferenceActivity;  
  5.   
  6. public class MainActivity extends PreferenceActivity  {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         addPreferencesFromResource(R.xml.mylistperference);  
  11.     }  
  12. }  

res/xml/checkbox.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:key="edittext_screen"  
  4.     android:summary="Screen Brief Description"  
  5.     android:title="Screen title" >  
  6.   
  7.     <EditTextPreference  
  8.         android:dialogTitle="Enter your name:"  
  9.         android:key="editWord"  
  10.         android:defaultValue="im human"  
  11.         android:summary="Brief description"  
  12.         android:title="Input name" >  
  13.     </EditTextPreference>  
  14.   
  15. </PreferenceScreen>  

V. Preferences Grouping

The obvious purpose is to be more beautiful. Categorize different types of options to improve user experience.

The first is to create two (multi) level pages. Detailed options are only visible when you point in, as shown in the figure:


MainActivity remains unchanged, res/xml/checkbox.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:key="edittext_screen"  
  4.     android:summary="Screen Brief Description"  
  5.     android:title="Screen title" >  
  6.   
  7.     <!-- first group -->  
  8.     <PreferenceScreen  
  9.         xmlns:android="http://schemas.android.com/apk/res/android"  
  10.         android:key="edittext_screen"  
  11.         android:summary="Click to enter the first set of preferences"  
  12.         android:title="first group" >  
  13.         <EditTextPreference  
  14.             android:dialogTitle="Enter your name:"  
  15.             android:key="editTitlePreference1"  
  16.             android:summary="Brief description"  
  17.             android:title="Input name" >  
  18.         </EditTextPreference>  
  19.     </PreferenceScreen>  
  20.   
  21.     <!-- Second group -->  
  22.     <PreferenceScreen  
  23.         xmlns:android="http://schemas.android.com/apk/res/android"  
  24.         android:key="edittext_screen"  
  25.         android:summary="Click to enter the second set of preferences"  
  26.         android:title="Second group" >  
  27.         <EditTextPreference  
  28.             android:dialogTitle="Enter your name:"  
  29.             android:key="editTitlePreference2"  
  30.             android:summary="Brief description"  
  31.             android:title="Input name" >  
  32.         </EditTextPreference>  
  33.     </PreferenceScreen>  
  34.   
  35.     <!-- Third group -->  
  36.     <PreferenceScreen  
  37.         xmlns:android="http://schemas.android.com/apk/res/android"  
  38.         android:key="edittext_screen"  
  39.         android:summary="Click to enter the third group of preferences"  
  40.         android:title="Third group" >  
  41.         <EditTextPreference  
  42.             android:dialogTitle="Enter your name:"  
  43.             android:key="editTitlePreference3"  
  44.             android:summary="Brief description"  
  45.             android:title="Input name" >  
  46.         </EditTextPreference>  
  47.     </PreferenceScreen>  
  48.   
  49. </PreferenceScreen>  

As you can see, Preference Screen is essentially a hierarchical tag.

What if we have fewer preferences, but we still want to group them?

We can change the nested Preference Screen in the above code to Preference Category, which is as simple as that! The code does not work, the effect is as follows:



The above parts are reproduced or referenced from the following sources:

http://www.eoeandroid.com/thread-5305-1-1.html

http://www.pocketdigi.com/20110405/231.html

http://blog.csdn.net/flowingflying/article/details/6671548

http://blog.csdn.net/chenzheng_java/article/details/6285966

http://blog.csdn.net/chenzheng_java/article/details/6286037

http://blog.csdn.net/chenzheng_java/article/details/6286115

http://blog.csdn.net/chenzheng_java/article/details/6286195

Topics: Android xml encoding Google