Sometimes, our programs need to provide options for users to customize their own use styles. For example, you may allow users to automatically save logon information, set the page refresh time, and so on. On the Android platform, we can use the PreferenceActivity base class to display the user with an option setting interface. In Android3.0 or later versions, you can use the PreferenceFragment class to implement this function.
The following describes how to create and use PreferenceFragment.
1. Create a project: PreferenceFragmentExample.
2. Create an xml folder under the res folder and a file preferences. xml under the xml folder.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<PreferenceScreen
Xmlns: android = "http://schemas.android.com/apk/res/android">
<PreferenceCategory android: title = "Category 1">
<CheckBoxPreference
Android: title = "Checkbox"
Android: defaultValue = "false"
Android: summary = "True of False"
Android: key = "checkboxPref"/>
</PreferenceCategory>
<PreferenceCategory android: title = "Category 2">
<EditTextPreference
Android: name = "EditText"
Android: summary = "Enter a string"
Android: defaultValue = "[Enter a string here]"
Android: title = "Edit Text"
Android: key = "editTextPref"/>
<RingtonePreference
Android: name = "Ringtone Preference"
Android: summary = "Select a ringtone"
Android: title = "Ringtones"
Android: key = "ringtonePref"/>
<PreferenceScreen
Android: title = "Second Preference Screen"
Android: summary =
"Click here to go to the second Preference Screen"
Android: key = "secondPrefScreenPref">
<EditTextPreference
Android: name = "EditText"
Android: summary = "Enter a string"
Android: title = "Edit Text (second Screen )"
Android: key = "secondEditTextPref"/>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
3. Create a class Fragment1.java under the package path.
[Html]
Public class Fragment1 extends PreferenceFragment {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Load options from xml files
AddPreferencesFromResource (R. xml. preferences );
}
}
4. PreferenceFragmentExampleActivity. java (main activity) code.
[Java]
Public class PreferenceFragmentExampleActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction =
FragmentManager. beginTransaction ();
Fragment1 fragment1 = new Fragment1 ();
FragmentTransaction. replace (android. R. id. content, fragment1 );
FragmentTransaction. addToBackStack (null );
FragmentTransaction. commit ();
}
}
5. Press F11 to debug the simulator.
6. Click the second line to display a window.
7. Use the DDMS tool to generate an xml file under the package path of the application.
To create a list style option, first create a preferences. xml file and then fill in various elements in the file.
Then, we need a subclass that inherits the PreferenceFragment:
[Html]
Public class Fragment1 extends PreferenceFragment {}
Next, we use addPreferencesFromResource () to load the xml file:
[Html]
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Load options from xml files
AddPreferencesFromResource (R. xml. preferences );
}
Finally, we need to display Fragment1 in the activity. In this case, we need to use FragmentManager and FragmentTrasaction:
[Java]
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction =
FragmentManager. beginTransaction ();
Fragment1 fragment1 = new Fragment1 ();
FragmentTransaction. replace (android. R. id. content, fragment1 );
FragmentTransaction. addToBackStack (null );
FragmentTransaction. commit ();
Here we will talk about fragment. The next part will explain the menu. Very simple. You are welcome to give your valuable comments.