Preference Use Details

Posted by angelac on Tue, 06 Aug 2019 11:18:45 +0200

Strongly Recommend Articles: Welcome to Collection
Android Dry Goods Sharing

Read for five minutes, ten o'clock a day, and study with you for life. Here's Android, a programmer.

This article mainly introduces some knowledge points in Android development. By reading this article, you will gain the following contents:

  1. Introduction to Perference
  2. Perference usage method
  3. Defining Preference with XML
  4. Defining Preference with Fragment
  5. Achieving results

Preference stayAndroid One of the important controls, such as Settings Most of the modules are passed throughPreference Realized, this chapter will studypreference Usage method

1.Preference brief introduction

Preference Inheritance relationship

java.lang.Object
    ↳ android.preference.Preference 

Preference is often used in the APP settings module, such as the Settings module in Android system, which can store our data in Share Preference by default.

  • The key-values acquisition method is the same as SharePreference
        SharedPreferences    mSp = PreferenceManager.getDefaultSharedPreferences(this);
        String favPhone = mSp.getString(PREF_LISTPHONE, "MI");
  • Preference Data Preservation

Preference is saved in the form of key-values.
The save path is as follows:
/ data/data/package name/shared_prefs/package name_preferences.xml

2. Perference usage

Settings are typically laid out by creating xml files in xml folders. Preference Screen container is usually used in layout files. Preference Screen contains many Preference controls. Preference Category can set classification headings.

Common Preference controls are as follows:
PreferenceScreen
CheckBoxPreference
EditTextPreference
ListPreference
PreferenceCategory
RingtonePreference.

The control above is equivalent to View in Layout layout, and Preference Screen is equivalent to ViewGroup in Layout layout. Before Android 3.0, you need to inherit PreferenceActivity (this method is not too recommended and has been enabled), and after 3.0, you need to inherit Activity, use Fragment to control layout, and then implement different controls to handle events.

Perference is commonly used in two ways:

    1. Defining Preference with XML
    1. Defining Preference with Fragment

3. Define Preference with XML

This method is a bit outdated and not recommended, but it is still available.
The method of use is as follows:

    1. Save the XML file in res/xml/directory, such as preferences.xml
    2. Inheriting PreferenceActivity directly calls addPreferences FromResource (R.xml.preference) in onCreate method; adding layout
    1. Click on event handling setOn Preference ClickListener, content change event handling setOn Preference ChangeListener

    The use cases of Preference are as follows:
    res /xml /preferences.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <Preference
            android:key="about_phone"
            android:summary="@string/pref_about_phone_sum"
            android:title="@string/pref_about_phone_tittle" />
    
        <SwitchPreference
            android:key="sw_perference"
            android:summary="@string/pref_about_phone_sum"
            android:title="@string/pref_about_phone_tittle" />
    
        <RingtonePreference
            android:key="pref_ring"
            android:summary="@string/pref_ringtone_tittle"
            android:title="@string/pref_ringtone_sum" />
    
        <CheckBoxPreference
            android:defaultValue="true"
            android:icon="@null"
            android:key="pref_blue"
            android:summary="@string/preference_blue_sum"
            android:title="@string/preference_blue_tittle"
            android:widgetLayout="@layout/test" />
    
        <PreferenceCategory
            android:key="pref_fav_category"
            android:title="@string/pref_fav_category_tittle" >
            <EditTextPreference
                android:key="fav_city"
                android:summary="@string/pref_ed_name_sum"
                android:title="@string/pref_ed_name_tittle" />
    
            <ListPreference
                android:dialogTitle="@string/pref_fav_title"
                android:entries="@array/fav_phone"
                android:entryValues="@array/fav_phone"
                android:key="pref_fav_phone"
                android:summary="@string/pref_fav_sum"
                android:title="@string/pref_fav_title" />
        </PreferenceCategory>
        <PreferenceCategory
            android:key="pref_contact_category"
            android:title="@string/pref_fav_contact_tittle" />
    
        <PreferenceScreen
            android:key="pre_voicemail_setting_key"
            android:persistent="false"
            android:title="@string/pref_voicemail_tittle" >
            <Preference
                android:key="voice_mail"
                android:summary="@string/pref_voicemail_tittle"
                android:title="@string/pref_voicemail_sum" >
                <intent
                    android:action="android.intent.action.VIEW"
                    android:data="http://www.baidu.com" />
            </Preference>
        </PreferenceScreen>
    
    </PreferenceScreen>

    Activity processing method

    public class PreferenceMethods extends PreferenceActivity implements
            OnPreferenceClickListener, OnPreferenceChangeListener {
    
        public static final String PREF_BLUE = "pref_blue";
        public static final String PREF_LISTPHONE = "pref_fav_phone";
        public static final String PREF_CITY = "fav_city";
        public static final String PREF_RING = "pref_ring";
    
        private CheckBoxPreference mBlueCheckPreference;
        private ListPreference mFavPhoneListPreference;
        private EditTextPreference mFavCityEdPreference;
        private RingtonePreference mRingtonePreference;
        private SwitchPreference mSwitchPreference;
        private SharedPreferences mSp;
        boolean isCheck;
    
        @SuppressLint("NewApi")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preference);
            mSp = PreferenceManager.getDefaultSharedPreferences(this);
    
            isCheck = mSp.getBoolean(PREF_BLUE, true);
            String favPhone = mSp.getString(PREF_LISTPHONE, "MI");
    
            mBlueCheckPreference = (CheckBoxPreference) findPreference(PREF_BLUE);
            mFavPhoneListPreference = (ListPreference) findPreference(PREF_LISTPHONE);
            mFavCityEdPreference = (EditTextPreference) findPreference(PREF_CITY);
            mRingtonePreference = (RingtonePreference) findPreference(PREF_RING);
            mSwitchPreference = (SwitchPreference) findPreference("sw_perference");
            // mSwitchPreference.setEnabled(false);
            mSwitchPreference.setChecked(true);
            mSwitchPreference.setShouldDisableView(true);
    
            mFavPhoneListPreference.setSummary("dddd");
            mFavPhoneListPreference.setOnPreferenceClickListener(this);
            mFavPhoneListPreference.setOnPreferenceChangeListener(this);
            mFavPhoneListPreference.setValueIndex(3);
    
            mBlueCheckPreference.setChecked(isCheck);
            mBlueCheckPreference.setOnPreferenceClickListener(this);
            mBlueCheckPreference.setOnPreferenceChangeListener(this);
    
            String favCity = mSp.getString(PREF_CITY, "Chengdu");
            mFavCityEdPreference.setOnPreferenceClickListener(this);
            mFavCityEdPreference.setOnPreferenceChangeListener(this);
            mFavCityEdPreference.setSummary(favCity);
    
            String favRingtone = mSp.getString(PREF_RING, "Decade");
            mRingtonePreference.setSummary(favRingtone);
            mRingtonePreference.setOnPreferenceChangeListener(this);
            mRingtonePreference.setOnPreferenceClickListener(this);
        }
    
        @Override
        public boolean onPreferenceClick(Preference preference) {
            return true;
        }
    
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
    
            if (mBlueCheckPreference.equals(preference)) {
                isCheck = !isCheck;
                mBlueCheckPreference.setChecked(isCheck);
            }
    
            if (mFavPhoneListPreference.equals(preference)) {
                mFavPhoneListPreference.setSummary(newValue.toString());
            }
    
            if (mFavCityEdPreference.equals(preference)) {
                mFavCityEdPreference.setSummary(newValue.toString());
            }
            if (preference.equals(mRingtonePreference)) {
    
                mRingtonePreference.setSummary(newValue.toString());
            }
            return true;
        }
    }

    4. Define Preference with Fragment

    This method is recommended for use. If you need to know how to use Fragment s, see Fragment Use Details

      1. First customize Fragment fragments

    a. Custom Settings Fragment

    public class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preference);
        }
    }
    

    b.preference implementation

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <Preference
            android:key="about_phone"
            android:summary="@string/pref_about_phone_sum"
            android:title="@string/pref_about_phone_tittle" />
    
        <SwitchPreference
            android:key="sw_perference"
            android:summary="@string/pref_about_phone_sum"
            android:title="@string/pref_about_phone_tittle" />
    
        <RingtonePreference
            android:key="pref_ring"
            android:summary="@string/pref_ringtone_tittle"
            android:title="@string/pref_ringtone_sum" />
    
        <CheckBoxPreference
            android:defaultValue="true"
            android:icon="@null"
            android:key="pref_blue"
            android:summary="@string/preference_blue_sum"
            android:title="@string/preference_blue_tittle"
            android:widgetLayout="@layout/test" />
    
        <PreferenceCategory
            android:key="pref_fav_category"
            android:title="@string/pref_fav_category_tittle" >
            <EditTextPreference
                android:key="fav_city"
                android:summary="@string/pref_ed_name_sum"
                android:title="@string/pref_ed_name_tittle" />
    
            <ListPreference
                android:dialogTitle="@string/pref_fav_title"
                android:entries="@array/fav_phone"
                android:entryValues="@array/fav_phone"
                android:key="pref_fav_phone"
                android:summary="@string/pref_fav_sum"
                android:title="@string/pref_fav_title" />
        </PreferenceCategory>
        <PreferenceCategory
            android:key="pref_contact_category"
            android:title="@string/pref_fav_contact_tittle" />
    
        <PreferenceScreen
            android:key="pre_voicemail_setting_key"
            android:persistent="false"
            android:title="@string/pref_voicemail_tittle" >
            <Preference
                android:key="voice_mail"
                android:summary="@string/pref_voicemail_tittle"
                android:title="@string/pref_voicemail_sum" >
                <intent
                    android:action="android.intent.action.VIEW"
                    android:data="http://www.baidu.com" />
            </Preference>
        </PreferenceScreen>
    
    </PreferenceScreen>
      1. Calling Fragment in Activity
    public class SettingPreferenceActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_preference);
    
            getFragmentManager().beginTransaction()
                    .replace(R.id.fm_pref, new SettingsFragment()).commit();
        }
    
    }
    • Filling layout
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <FrameLayout
            android:id="@+id/fm_pref"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>

    5. The results are as follows:

    So far, this article is over. If there are any mistakes, you are welcome to make suggestions and corrections. At the same time look forward to your attention, thank you for reading, thank you!

    Topics: Android xml Fragment encoding