Preferenceactivity commonly used in Android settings

Source: Internet
Author: User

Preferenceactivity contains many items similar to controls to indicate different setting requirements, such as classification setting, selection box setting, drop-down list setting, edit box setting, and ringtone setting, in addition, these settings do not need to be used to save the data set by users. They will save the data themselves in the XML file. The following describes the components in preferenceactivity.

1. preferencescreen: set page, which can be nested to form a second-level setting page. Set the title with the title parameter.


Start activity in preferencescreen
For example, wireless_setting.xml contains the following fragments:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"xmlns:settings="http://schemas.android.com/apk/res/com.seedshope.android">    <PreferenceScreen        android:key="wifi_settings"        android:title="@string/wifi_settings"        android:summary="@string/wifi_settings_summary" >        <intent            android:action="android.intent.action.MAIN"            android:targetPackage="com.android.settings"            android:targetClass="com.android.settings.wifi.WifiSettings" />    </PreferenceScreen></PreferenceScreen>

Com. Android. settings is the package name of the project, and COM. Android. settings. Wifi. wifisettings is the class to be started.
In general, it is okay to click the corresponding preference to start the corresponding activity. However, in this case, sometimes the corresponding activity may not be started, because it is related to the callback function onpreferencetreeclick, sometimes we will rewrite this method as follows:

public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {       if ( (preference == mAirplaneModePreference) &&                (Boolean.parseBoolean(                    SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) ) {            // In ECM mode launch ECM app dialog           startActivityForResult(                new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),                REQUEST_CODE_EXIT_ECM);            return true;        }        else {            // Let the intents be launched by the Preference manager            return false;       }    }

Wirelesssetting. java has the above function. If the returned value is true, you will not jump to the activity when you click preference. You can jump to the activity normally only when the returned value is false, because the actual implementation of clicking a preference is in preference. java's javasmclick function is as follows:

void performClick(PreferenceScreen preferenceScreen) {                if (!isEnabled()) {            return;        }                onClick();                if (mOnClickListener != null && mOnClickListener.onPreferenceClick(this)) {            return;        }                PreferenceManager preferenceManager = getPreferenceManager();        if (preferenceManager != null) {            PreferenceManager.OnPreferenceTreeClickListener listener = preferenceManager                    .getOnPreferenceTreeClickListener();            if (preferenceScreen != null && listener != null                    && listener.onPreferenceTreeClick(preferenceScreen, this)) {                return;            }        }                if (mIntent != null) {            Context context = getContext();            context.startActivity(mIntent);        }    }

When you click a preference, the calling process is adapterview. extends mitemclick -- "preferencescreen. onitemclick -- "preference. extends mclick -- "preferenceactivity. onpreferencetreeclick: When onpreferencetreeclick returns true, it returns directly and does not go through the following to start the activity. Therefore, there are two conditions for a preference to jump to another activity, the first is whether the settings in the XML are correct, and the second is whether the Java class that calls the XML is in the onpreferencetreeclick function. False must be returned.


2. preferencecategory: a type of related settings. The title can be set by the title parameter, which is equivalent to the same thing as a demarcation line. The above and below are different types of settings.

3. checkboxpreference: it is a checkbox setting with only two values, true or false. You can use the title parameter to set the title, and use the summaryon and summaryoff parameters to set the prompt in the selected or unselected controls, you can use defaultvalue to set the default value.

4. listpreference: select the control from the drop-down list, set the title with the title parameter, set the description with the summary parameter, click it, and a drop-down box is displayed. Use dialogtitle to set the title of the drop-down box, the content and specific values displayed in the drop-down list must be in RES/values/array. set two arrays in XML to indicate, entries and entryvalues respectively indicate the displayed value and the real value obtained in the code. For example, set array. XML as follows:

<?xml version ="1.0" encoding ="utf-8" ?><resources><string-array name ="entries_list_preference" >    <item> test1</item>    <item> test2</item>    <item> test3</item></string-array><string-array name ="entriesvalue_list_preference" >    <item> 1</item>    <item> 2</item>    <item> 3</item></string-array></resources>

5. edittextpreference: Controls in the input box. After clicking, you can enter string settings. Use the title parameter to set the title, summary parameter setting instructions, and dialogtitle to set the title of the input box.

6. ringtonepreference: select the ringtone box. After you click it, select the system ringtone. Title parameter settings title, summary parameter settings description, dialogtitle parameter settings ringtone selection box title.

7. The above is the XML Description of preferenceactivity. in the program, we only need to create an activity inherited from preferenceactivity, and then call it in the main program. The storage settings in this preferenceactivity are completely automatic, and you do not need to use code to implement the storage settings. After preferenceactivity is created, a configuration file/data/your_package_name is automatically created.
/Shared_prefs/(your_package_name) _ preferences. xml. The automatically generated configuration file is as follows:

<?xml version ='1.0' encoding ='utf-8' standalone ='yes' ?><map><string name ="EditTextPreference" >12332312</string><string name ="ListPreference" >2</string><string name ="RingtonePreference" >content://media/internal/audio/media/10</string><boolean name ="CheckBox1" value ="true" /><boolean name ="CheckBox2" value ="true" /></map>

The name attribute in the file corresponds to the key attribute in the XML file, which indicates a certain setting item. The value is unique, and the value in the middle of the value and string indicates the specific value of the Setting item.

Some configurations of adwlauncher are as follows:

<PreferenceScreenandroid:key="screenPreferences"android:title="@string/pref_title_screen_preferences"android:summary="@string/pref_summary_screen_preferences"><CheckBoxPreference android:title="@string/pref_title_hide_statusbar"android:defaultValue="false"android:summary="@string/pref_summary_hide_statusbar"android:key="hideStatusbar" /><CheckBoxPreference android:title="@string/pref_title_wallpaper_scrolling"android:defaultValue="@bool/config_wallpaper_scroll"android:summary="@string/pref_summary_wallpaper_scrolling"android:key="wallpaper_scrolling" /><PreferenceCategoryandroid:title="@string/pref_title_screens_layout_preferencecategory"><CheckBoxPreference android:key="uiHideLabels"android:title="@string/pref_title_hide_icon_labels"android:summary="@string/pref_summary_hide_icon_labels"android:defaultValue="false" /><CheckBoxPreference android:title="@string/pref_title_autosize_icons"android:defaultValue="false"android:summary="@string/pref_summary_autosize_icons"android:key="autosizeIcons" /><com.android.launcher.DialogSeekBarPreferenceandroid:title="@string/pref_title_desktop_columns"android:summary="@string/pref_summary_desktop_columns"android:dialogMessage="@string/pref_dialog_desktop_columns"android:key="desktopColumns" android:defaultValue="@integer/config_desktopColumns"android:max="5" /><com.android.launcher.DialogSeekBarPreferenceandroid:title="@string/pref_title_desktop_rows"android:summary="@string/pref_summary_desktop_rows"android:dialogMessage="@string/pref_dialog_desktop_rows"android:key="desktopRows" android:defaultValue="@integer/config_desktopRows"android:max="5" /></PreferenceCategory><PreferenceCategory            android:title="@string/pref_title_screens_behaviour_preferencecategory"><com.android.launcher.DialogSeekBarPreferenceandroid:title="@string/pref_title_desktop_scrolling_speed"android:summary="@string/pref_summary_desktop_scrolling_speed"android:dialogMessage="@string/pref_dialog_desktop_scrolling_speed"android:key="desktopSpeed" android:defaultValue="@integer/config_desktopSpeed"android:max="2000" /><com.android.launcher.DialogSeekBarPreferenceandroid:title="@string/pref_title_desktop_overshoot"android:summary="@string/pref_summary_desktop_overshoot"android:dialogMessage="@string/pref_dialog_desktop_overshoot"android:key="desktopBounce" android:defaultValue="@integer/config_desktopBounce"android:max="100" /></PreferenceCategory></PreferenceScreen>

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.