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:
-
package com.plusjun.preference;
-
-
import android.os.Bundle;
-
import android.preference.PreferenceActivity;
-
-
-
-
-
public class MainActivity extends PreferenceActivity {
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
-
-
-
-
-
-
-
-
-
addPreferencesFromResource(R.xml.mylistperference);
-
}
-
}
res/xml/mylistperference.xml layout file
-
<?xml version="1.0" encoding="utf-8"?>
-
<!--
-
The following points should be noted for this document.
-
First: location. The location of this file is under res/xml/.
-
Second: Format, Preference Screen is the root tag, List Preference is the subtag. Preference Screen can be seen as a large framework.
-
Third: the meaning of label attributes
-
android:key unique identifier, similar to android:id, which Preference Manager can use as a parameter to obtain the specified preference through find Preference
-
Note that the value of this android:key is also the XML "key" name in the Preference file.
-
android:title headline
-
android:summary: The small word under the heading (this is available as a tab)
-
In the android:entries pop-up dialog box, the list shows the text content. Note that an array is specified here.
-
android:entryValues corresponding to android:entries
-
android:defaultValue
-
Android: The title information in the dialogTitle pop-up dialog box
-
More label settings await your discovery.
-
-->
-
<PreferenceScreen
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:key="screen_list"
-
android:title= "title"
-
android:summary= "summary of instructions"
-
>
-
<ListPreference
-
android:key="myListPreference"
-
android:title= "title"
-
android:summary= "summary of instructions"
-
android:entries="@array/list_entries"
-
android:entryValues="@array/list_entries_value"
-
android:dialogTitle= "Here is the pop-up box"
-
android:defaultValue="@string/deflault_list_entries_value"
-
></ListPreference>
-
-
</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:
-
<string-array name="list_entries_value">
-
<item>shandong</item>
-
<item>fujian</item>
-
<item>beijing</item>
-
<item>hebei</item>
-
</string-array>
-
-
<string-array name="list_entries">
-
<item>Shandong</item>
-
<item>Fujian</item>
-
<item>Beijing</item>
-
<item>Hebei</item>
-
</string-array>
-
-
<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:
-
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
-
<map>
-
<string name="myListPreference">fujian1</string>
-
</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:
-
package com.plusjun.preference;
-
-
import android.os.Bundle;
-
import android.preference.PreferenceActivity;
-
-
public class MainActivity extends PreferenceActivity {
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
addPreferencesFromResource(R.xml.mylistperference);
-
}
-
}
res/xml/checkbox.xml layout file
-
<?xml version="1.0" encoding="utf-8"?>
-
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-
android:key="screen_list"
-
android:summary="Explanatory summary"
-
android:title="Title" >
-
-
<CheckBoxPreference
-
android:key="medf"
-
android:summaryOff="Maldives not selected"
-
android:summaryOn="Maldives was selected"
-
android:title="Maldives" >
-
</CheckBoxPreference>
-
<CheckBoxPreference
-
android:key="mlqs"
-
android:summaryOff="Mauritius was not selected"
-
android:summaryOn="Mauritius was selected"
-
android:title="Mauritius" >
-
</CheckBoxPreference>
-
-
</PreferenceScreen>
Finally, the file content generated in "<Project Name>_Preference.xml":
-
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
-
<map>
-
<boolean name="medf" value="true" />
-
<boolean name="mlqs" value="false" />
-
</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:
-
package com.plusjun.preference;
-
-
import android.os.Bundle;
-
import android.preference.PreferenceActivity;
-
-
public class MainActivity extends PreferenceActivity {
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
addPreferencesFromResource(R.xml.mylistperference);
-
}
-
}
res/xml/checkbox.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-
android:key="edittext_screen"
-
android:summary="Screen Brief Description"
-
android:title="Screen title" >
-
-
<EditTextPreference
-
android:dialogTitle="Enter your name:"
-
android:key="editWord"
-
android:defaultValue="im human"
-
android:summary="Brief description"
-
android:title="Input name" >
-
</EditTextPreference>
-
-
</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:
-
<?xml version="1.0" encoding="utf-8"?>
-
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-
android:key="edittext_screen"
-
android:summary="Screen Brief Description"
-
android:title="Screen title" >
-
-
-
<PreferenceScreen
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:key="edittext_screen"
-
android:summary="Click to enter the first set of preferences"
-
android:title="first group" >
-
<EditTextPreference
-
android:dialogTitle="Enter your name:"
-
android:key="editTitlePreference1"
-
android:summary="Brief description"
-
android:title="Input name" >
-
</EditTextPreference>
-
</PreferenceScreen>
-
-
-
<PreferenceScreen
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:key="edittext_screen"
-
android:summary="Click to enter the second set of preferences"
-
android:title="Second group" >
-
<EditTextPreference
-
android:dialogTitle="Enter your name:"
-
android:key="editTitlePreference2"
-
android:summary="Brief description"
-
android:title="Input name" >
-
</EditTextPreference>
-
</PreferenceScreen>
-
-
-
<PreferenceScreen
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:key="edittext_screen"
-
android:summary="Click to enter the third group of preferences"
-
android:title="Third group" >
-
<EditTextPreference
-
android:dialogTitle="Enter your name:"
-
android:key="editTitlePreference3"
-
android:summary="Brief description"
-
android:title="Input name" >
-
</EditTextPreference>
-
</PreferenceScreen>
-
-
</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