1. "First Encounter"
The application we build often requires a setting interface to set parameters of the application, just like the setting interface of the Android system.
Android provides a dedicated method to build your own setting Interface
2. "first look at the appearance"
First create a project
Add the XML folder under the res directory of the project and add the XML file for the build setting Interface
Let's edit the layout file setting_demo.xml.
<? XML version = "1.0" encoding = "UTF-8"?> <Preferencescreen xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Key = "setting_demo_preference" Android: Title = "@ string/title_setting_demo_preference" Android: summary = "@ string/summary_setting_demo_preference"> <preferencecategory xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Key = "common_settings" Android: Title = "@ string/common_settings"> <checkboxpreference Android: key = "check_box" Android: Title = "@ string/title_check_box" Android: Summary = "@ string/summary_check_box" Android: Persistent = "false"/> <listpreference Android: key = "string_list_preference" Android: Title = "@ string/title_list_preference" Android: Summary = "@ string/summary_list_preference" Android: entries = "@ array/entries_list_preference" Android: entryvalues = "@ array/entryvalues_list_preference" Android: dialogtitle = "@ string/dialog_title_list_preference"/> <edittextpreference Android: Key = "edit_text_preference" Android: title = "@ string/example" Android: Summary = "@ string/summary_edit_text_preference" Android: dialogtitle = "@ string/example"/> </preferencecategory> <preferencecategory xmlns: android = "http://schemas.android.com/apk/res/android" Android: Key = "advanced_settings" Android: Title = "@ string/advanced_settings"> <volumepreference Android: Key = "media_volume" Android: title = "@ string/media_volume_title" Android: Summary = "@ string/media_volume_summary" Android: dialogtitle = "@ string/media_volume_title" Android: Persistent = "false" Android: order = "3" Android: streamtype = "Music"/> <seekbarpreference Android: Key = "seek_bar_preference" Android: title = "@ string/seek_bar_preference_title"> </seekbarpreference> </preferencecategory> </preferencescreen>
The string definition:
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, settingdemo! </String> <string name = "app_name"> settingdemo </string> <string name = "title_setting_demo_preference"> setting demo </string> <string name = "summary_setting_demo_preference"> setting demo summary </string> <string name = "title_check_box"> title check box </string> <string name = "summary_check_box"> summary check box </string> <string name = "title_list_preference"> title_list_preference </string> <string name = "summary_list_preference"> summary_list_preference </string> <string name = "deny"> deny </string> <string name =" secret "> title_edit_text_preference </string> <string name =" summary_edit_text_preference "> summary_edit_text_preference </string> <string name =" comment "> comment </string> <string name =" common_settings "> General Settings </string> <string name =" advanced_settings "> Advanced Settings </string> <string name =" media_volume_title "> media_volume_title </string> <string name =" media_volume_summary "> media_volume_summary </string> <string name =" seek_bar_preference_title "> seek_bar_preference </string> </resources>
After editing these two files, we can add the following sentence to oncreate in settingdemo. Java to see the effect.
Public class settingdemo extends preferenceactivity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); addpreferencesfromresource (R. XML. setting_demo );}}
Shows the effect. You can easily understand the usage of each label in the layout file.
3. "in-depth understanding"
Now, the option in the settings does not have any substantial effect. First, let's take a look at the checkbox control in the settings.
Private Static final string check_box_pre = "check_box"; // corresponds to private checkboxpreference mcheckboxpre with the key value in XML; // declares an object mcheckboxpre = (checkboxpreference) used to control the checkbox) findpreference (check_box_pre); // findpreference finds the Control <checkboxpreference Android: Key = "check_box" Android: Title = "@ string/title_check_box" Android: summary = "@ string/summary_check_box" Android: Persistent = "false"/> mcheckboxpre. setonpreferencechangelistener (this); // sets the listener. We will process the setting changes in onpreferencechange.
The Code is as follows:
public class SettingDemo extends PreferenceActivity implementsOnPreferenceChangeListener {private static final String CHECK_BOX_PRE = "check_box";private CheckBoxPreference mCheckBoxPre;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.setting_demo);mCheckBoxPre = (CheckBoxPreference) findPreference(CHECK_BOX_PRE);mCheckBoxPre.setOnPreferenceChangeListener(this);}@Overridepublic boolean onPreferenceChange(Preference preference, Object newValue) {// TODO Auto-generated method stubif (preference.getKey().equals(CHECK_BOX_PRE)) {mCheckBoxPre.setChecked((Boolean) newValue);mCheckBoxPre.setSummary((Boolean) newValue ? "checked": "unchecked");doSomething((Boolean) newValue ? 1 : 0);}return false;}private void doSomething(int i) {// TODO Auto-generated method stubif (i == 1) {Toast.makeText(this, "checked!", Toast.LENGTH_SHORT).show();} else if (i == 0) {Toast.makeText(this, "unchecked!", Toast.LENGTH_SHORT).show();}}}
Run the command to see the effect!
In the program, we can also dynamically control the display of content in settings.
Preference yoursetting = findpreference (key_your_setting );
Getpreferencescreen (). removepreference (yoursetting );
In XML, we can also use intent to start another activity.
<Preferencescreen
Android: Title = "@ string/manageapplications_settings_title"
Android: Summary = "@ string/manageapplications_settings_summary">
<Intent Android: Action = "android. Intent. Action. Main"
Android: targetpackage = "com. Android. Settings"
Android: targetclass = "com. Android. settings. manageapplications"/>
</Preferencescreen>
Specify the package name and class name to start the specified activity.
Of course, you can also implicitly start the activity that can receive the intent by specifying the action. You can search for the intent and find a detailed explanation. Here we will not go over it.