PreferenceActivity, PreferenceFragment, preferenceactivity
Directory
- Directory
- Preface
- PreferenceActivity
- Preferences_scenario_1xml
- Preference Activity
- Demo
- PreferenceFragment
- Xml layout File
- Preference Fragment
- Preference Activity management Fragment
- Adaptation
Preface
In other words, I went back to Android. Here I will refer to the Settings application of Android native to introduce PreferenceActivity, PreferenceFragment, and headers.
PreferenceActivity
Let's take a simple example to learn how to use PreferenceActivity.
Preferences_scenario_1.xml
We first define a simple preference interface through the xml file, res/xml/preference_scenario_1.xml:
<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceScreen android:summary="@string/next_screen_title" android:title="@string/next_screen_summary" > <CheckBoxPreference android:key="@+id/next_screen_checkbox_preference" android:summary="@string/checkbox_summary" android:title="@string/checkbox_title" /> </PreferenceScreen> <PreferenceCategory android:title="@string/category_title" android:summary="@string/category_title"> <CheckBoxPreference android:defaultValue="false" android:key="pref_otificationskey" android:title="@string/checkbox_title" > </CheckBoxPreference> <ListPreference android:entries="@array/people_name" android:entryValues="@array/people_value" android:key="pref_SyncFrequencykey" android:summary="@string/list_summary" android:title="@string/list_title" > </ListPreference> </PreferenceCategory></PreferenceScreen>
The content of string. xml involved is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "next_screen_title"> next screen option </string> <string name = "next_screen_summary"> next screen </string> <string name = "checkbox_summary "> I am very handsome </string> <string name =" checkbox_title "> do I need to brush? </String> <string name = "category_title"> I like </string> <string name = "list_title"> name list </string> <string name = "list_summary"> my favorite names </string> </resources>
Preference Activity
PreferenceActivity is used to present Preference Screens before Android3.0. Therefore, if you want to support systems earlier than Android3.0, you must inherit the PreferenceActivity class to present Preference Screens. The sample code is as follows:
import android.os.Bundle;import android.preference.PreferenceActivity;public class CustomPreferenceActivity extends PreferenceActivity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preference_scenario_1); }}
Override the oncreate method and call the addPreferencesFromResource method to load the xml layout file.
Do not forget to declare this CustomPreferenceActivity in AndroidManifest. xml.
Note:
The addPreferencesFromResource method of PreferenceActivity has been deprecated after Android3.0 (api level 11. PreferenceFragment is recommended to present Preference Screen after Android3.0.
Demo
In other activities, you can use startActivity to start this PreferenceActivity, as shown in the following figure:
PreferenceFragment
Xml layout File
Xml file we can reuse the layout file of the PreferenceActivity above.
Preference Fragment
The sample code is as follows:
import android.os.Bundle;import android.preference.PreferenceFragment;public class CustomPreferenceFragment extends PreferenceFragment{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preference_scenario_1); }}
Preference Activity management Fragment
The sample code is as follows:
import android.os.Bundle;import android.preference.PreferenceActivity;public class CustomPreferenceActivity extends PreferenceActivity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); getFragmentManager().beginTransaction().replace(android.R.id.content, new CustomPreferenceFragment()).commit(); }}
Adaptation
To adapt to systems earlier than Android, we can use Build. VERSION. SDK_INT to automatically determine in the Code how to present Preference Screen. The sample code is as follows:
import android.os.Build;import android.os.Bundle;import android.preference.PreferenceActivity;public class CustomPreferenceActivity extends PreferenceActivity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { addPreferencesFromResource(R.xml.preference_scenario_1); } else { getFragmentManager().beginTransaction().replace(android.R.id.content, new CustomPreferenceFragment()).commit(); } }}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.