Reprinted: http://blog.chinaunix.net/uid-24666775-id-351136.html
In Android, apidemos often encounters the class preferenceactivity, followed by addpreferencesfromresource (R. XML. * ******); (Appendix: This ****** is an XML file, taking preference_dependencies as an example). Now we can see it in this XML file:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"><PreferenceCategoryandroid:title="@string/example_preference_dependency"><CheckBoxPreferenceandroid:key="wifi"android:title="@string/title_wifi" /><EditTextPreferenceandroid:layout="?android:attr/preferenceLayoutChild"android:title="@string/title_wifi_settings"android:dependency="wifi" /></PreferenceCategory></PreferenceScreen>
Preferencrscreen: Here I understand it as a window (probably for the sake of demonstration). The following are all his components, that is, it forms an interactive interface with the user (which will be displayed on a complete page). This XML file can be divided into two types: one is the display of management layout, such as preferencescreen; the other is specific settings, such as checkboxpreference and edittextprefrencr.
The above program also found that the preferencecategory attribute exists. The main function is to regard the content covered in it as one of its attributes. Other attributes are attached: The key is the preference ID, which can be referenced in the Code only after it is set. title is the title displayed, and summary is the text description displayed under the title. The effect is as follows:
The preferenceactivity setting interface is described above. When the content in the preferenceactivity changes, the android system will automatically save and maintain persistence. We only need to read the data in the settings interface to be used. Android also provides two preference-related listening interfaces, onpreferenceclicklistener and onpreferencechangelistener. When a preference in preferenceactivity performs a click or change operation, it calls back the functions in the interface, in this way, the system settings can be changed to other activities at the first time.
Another method is to use setpreferencescreen (preferencescreen) and write the preceding method;
public class PreferencesFromCode extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setPreferenceScreen(createPreferenceHierarchy()); } private PreferenceScreen createPreferenceHierarchy() { // Root PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this); // Inline preferences PreferenceCategory inlinePrefCat = new PreferenceCategory(this); inlinePrefCat.setTitle(R.string.inline_preferences); root.addPreference(inlinePrefCat); // Toggle preference CheckBoxPreference togglePref = new CheckBoxPreference(this); togglePref.setKey("toggle_preference"); togglePref.setTitle(R.string.title_toggle_preference); togglePref.setSummary(R.string.summary_toggle_preference); inlinePrefCat.addPreference(togglePref); // Dialog based preferences PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this); dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences); root.addPreference(dialogBasedPrefCat); // Edit text preference EditTextPreference editTextPref = new EditTextPreference(this); editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference); editTextPref.setKey("edittext_preference"); editTextPref.setTitle(R.string.title_edittext_preference); editTextPref.setSummary(R.string.summary_edittext_preference); dialogBasedPrefCat.addPreference(editTextPref); // List preference ListPreference listPref = new ListPreference(this); listPref.setEntries(R.array.entries_list_preference); listPref.setEntryValues(R.array.entryvalues_list_preference); listPref.setDialogTitle(R.string.dialog_title_list_preference); listPref.setKey("list_preference"); listPref.setTitle(R.string.title_list_preference); listPref.setSummary(R.string.summary_list_preference); dialogBasedPrefCat.addPreference(listPref); return root } }
Effect