To introduce this probability, first of all, we need to start with the requirement that an existing Activity is specially used for mobile phone attribute settings. What should we do?
Based on the learned knowledge, a thought quickly flashed through: activity + Preference combination: the former is used to build the interface, and the latter is used to set data storage. In fact, this is true, but this is complicated because each setting option must create a Preference corresponding to it.
So now we have a better choice, that is, the main character of this article: PreferenceActivity
From the name, we can see the mixture of Activity and Perference.
Which of the following methods can be used to set common attributes:
1. CheckBox is used to determine/cancel a function, such as: whether to mute corresponds to: CheckBoxPreference, which has an option in Preference and is automatically bound to it.
2. ListView is used to list all the choices. For example, the selection of ringtones lists all sounds for selection. Corresponding to: ListPreference
3. attributes are used together to list a series of related attributes. For example, all functions related to short messages correspond to: PreferenceScreen. This item is only used on the interface and is irrelevant to Preference.
In fact, we can see this on the interface, so we think it is still step-oriented.
Given that PreferenceActivity uses a lot of network resources, I would like to give a rough description of PreferenceActivity in order to back up and prevent it for myself.
[Code steps]
1. The PreferenceActivity interface is particularly applicable to res/xml/setting. xml.Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<PreferenceScreen
Xmlns: android = "http://schemas.android.com/apk/res/android">
<CheckBoxPreference
Android: key = "autoBack"
Android: title = "automatic reply"
Android: summaryOn = "enabled"
Android: summaryOff = "disabled"
Android: defaultValue = "true"
/>
<PreferenceCategory android: title = "SMS assistant">
<CheckBoxPreference
Android: key = "smsSilence"
Android: title = "mute"
Android: summaryOn = "enabled"
Android: summaryOff = "disabled"
Android: defaultValue = "false"
/>
<PreferenceScreen android: title = "more options">
<CheckBoxPreference
Android: key = "cb21"
Android: title = "feature 1"
Android: summaryOn = "enabled"
Android: summaryOff = "disabled"
Android: defaultValue = "true"
/>
<CheckBoxPreference
Android: key = "cb22"
Android: title = "feature 2"
Android: summaryOn = "use"
Android: summaryOff = "disabled"
Android: defaultValue = "true"
/>
<ListPreference
Android: key = "list1"
Android: title = "android forum"
Android: summary = "Development Forum"
Android: dialogTitle = "select Forum"
Android: entries = "@ array/entries_list_preference"
Android: entryValues = "@ array/entriesvalue_list_preference"
/>
<EditTextPreference
Android: key = "et1"
Android: title = "EditTextPreference Sample"
Android: summary = "click to enter"
Android: dialogTitle = "input Settings"
Android: defaultValue = "Hello EditTextPreference"
/>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android: title = "Other Options">
<RingtonePreference
Android: key = "rt1"
Android: title = "RingtonePreference Sample"
Android: summary = "select ringtone"
/>
</PreferenceCategory>
</PreferenceScreen>
2. Import setting. xmlCopy codeThe Code is as follows: addPreferencesFromResource (R. xml. settings );
Emulator run:
Click "more options"
Please try it by yourself
3. In fact, the problem of PreferenceActivity is basically over, but the first use will encounter some ups and downs.
4. Listener settings and changes the listener in the general layout is relatively simple. First, you can obtain the target View through findViewById () and then register the listener on it.
However, if the PreferenceActivity does not provide findViewById (), it cannot be monitored. How can this problem be solved?
-Get the SharedPreferences handle.Copy codeThe Code is as follows: sp = PreferenceManager. getdefasharsharedpreferences (this );
-Register and change the listener on the spCopy codeThe Code is as follows: sp. registerOnSharedPreferenceChangeListener (new OnSharedPreferenceChangeListener (){
@ Override
// SharedPreferences: handle
// Key: Change the key value
Public void onSharedPreferenceChanged (
SharedPreferences sharedPreferences, String key ){
// TODO Auto-generated method stub
If (key. equals ("autoBack ")){
Boolean autoBack = sharedPreferences. getBoolean ("autoBack", false );
If (autoBack ){
Log. d ("TAG", "autoBack: true! ");
}
Else {
Log. d ("TAG", "autoBack: false! ");
}
}
}
});
5. Preference Remote use: PreferenceActivity is in one application, while other applications may want to change the data. How can this problem be solved?
-Get the SharedPreferences instance through getSharedPreferences ()Copy codeThe Code is as follows: spc = this. getSharedPreferences ("com. android. PreferenceActivity. Usage_preferences.xml", MODE_WORLD_READABLE );
-ValueCopy codeThe Code is as follows: Boolean autoBack = spc. getBoolean ("autoBack", false );
-AssignmentCopy codeThe Code is as follows: public void putValue (String key, Boolean value ){
Spe = spc. edit ();
Spe. putBoolean (key, value );
Spe. commit ();
}
It seems uncomfortable to end this way, so I decided to add some materials.
-Use ListPreferenceCopy codeThe Code is as follows: <ListPreference
Android: key = "list1"
Android: title = "android forum"
Android: summary = "Development Forum"
Android: dialogTitle = "select Forum"
Android: entries = "@ array/entries_list_preference"
Android: entryValues = "@ array/entriesvalue_list_preference"
/>Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "entries_list_preference">
<Item> www.anddev.org </item>
<Item> www.eoeandroid.com </item>
<Item> pai.android.com </item>
</String-array>
<String-array name = "entriesvalue_list_preference">
<Item> 1 </item>
<Item> 2 </item>
<Item> 3 </item>
</String-array>
</Resources>
Use EditTextPreferenceCopy codeThe Code is as follows: <EditTextPreference
Android: key = "et1"
Android: title = "EditTextPreference Sample"
Android: summary = "click to enter"
Android: dialogTitle = "input Settings"
Android: defaultValue = "Hello EditTextPreference"
/>