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.
<?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.
Public class fragment1 extends preferencefragment {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // The addpreferencesfromresource (R. XML. preferences );}}
4. preferencefragmentexampleactivity. Java (main activity) code.
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:
public class Fragment1 extends PreferenceFragment {}
Next, we use addpreferencesfromresource () to load the XML file:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); }
Finally, we need to display fragment1 in the activity. In this case, we need to use fragmentmanager and fragmenttrasaction:
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.