Android Program Development: (13) Special Fragment-13.3 PreferenceFragment

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.