Introduction to Android preferenceactivity

Source: Internet
Author: User

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

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.