User options and settings user interface, option user interface

Source: Internet
Author: User

User options and settings user interface, option user interface

Many applications provide a separate interface that allows you to change the options and settings of an application. Android provides a set of ready-made Activity and Fragment classes, making it easy to create such user interfaces: preferenceActivity and preferenceFragment.

First, create an XML file in the XML Resource Directory and use the PreferenceScreen syntax at the beginning. The XML structure is simple. It specifies all the preferences that allow user changes and how they interact. Developers can provide simple text fields, check boxes, and option lists for entering text strings. For each option, you can specify the title and description, and divide the preference into different categories. The developer does not need to save the modified value, because PreferenceFragment will save the user's changes. The SharedPreferences instance used by PreferenceFragment is the same as that obtained from PreferenceManaget. getdefadefasharedpreferences.

The following PreferenceScreen. xml code shows the options that can be configured by two users.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">    <PreferenceCategory android:title="@string/network_preferences_title">        <CheckBoxPreference                android:title="@string/network_wifi_only_title"                android:summaryOn="@string/network_wifi_only_summary_on"                android:summaryOff="@string/network_wifi_only_summary_off"                android:key="network.wifiOnly"                android:defaultValue="false"/>        <ListPreference                android:title="@string/animal_retry_count_title"                android:summary="@string/animal_retry_count_summary"                android:key="network.retryCount"                android:defaultValue="3"                android:entryValues="@array/animal_retry_count_option_values"                android:entries="@array/animal_retry_count_options" />    </PreferenceCategory></PreferenceScreen>

The following describes how to implement PreferenceActivity and add preferenceFragment as its UI. Then, call PreferenceFragment. addPreferencesFromResource () to specify the XML for displaying the set user interface. The Android framework generates a user interface that conforms to the application style.

The following code specifies the XML resource file to be used. In this example, the PreferenceManager. setdefavaluvalues method is also called, so that the preference file uses the default value specified by the XML file.

Public class extends implements SharedPreferences. Sums {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); sums. setdefavaluvalues (getActivity (), R. xml.Preference_screen, False); addPreferencesFromResource (R. xml.Preference_screen); GetPreferenceScreen (). getSharedPreferences (). registerOnSharedPreferenceChangeListener (this) ;}@ Override public void onDestroy () {super. onDestroy (); getPreferenceScreen (). getSharedPreferences (). unregisterOnSharedPreferenceChangeListener (this) ;}@ Override public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {Log. I ("MainActivity", "liyuanjinglyj"); Log. I ("MainActivity", key); boolean flag = false; if ("network. wifiOnly ". equals (key) {flag = sharedPreferences. getBoolean (key, false);} if (flag) {Toast. makeText (getActivity (), "WIFI enabled", Toast.LENGTH_LONG). Show ();}}}
The code above not only implements the above functions but also implements SharedPreferences. onSharedPreferenceChangeListener interface. When the preference settings change, you can process the corresponding events based on the changed Key. Of course, you cannot monitor the changes in the preference settings by simply implementing this interface, you can only listen after registration.

GetPreferenceScreen (). getSharedPreferences (). registerOnSharedPreferenceChangeListener (this );

Of course, you must cancel the registration.

The most common way to start this type of Activity is to use Intent and specify ComponentName instead of the action string. Make sure that the android: exported flag is set to false in the configuration file so that the Activity can only be started in the application.



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.