Use PreferenceFragment to quickly implement app settings page

Now the app basically has some setting interface, some data needs to be saved to the server, and some data only needs to be saved locally. For the operation of saving to the local, I believe that everyone will generally use SharedPreference to implement, and then write a setting page by yourself, probably It took half a day to finish it, and I feel cute. As everyone knows, there is a simpler method, which is PreferenceFragment. You don't even need to write the interface layout yourself, and you can achieve an effect similar to the android setting interface. The key is to save time. I just tested it and started the project. It took less than 20 minutes to achieve results! ! ! And the biggest advantage is that it is completely in line with the Material Design officially launched by Google, and it is very unified and harmonious as a whole. Let's look at the effect first.





 
 

1. Create an xml resource file, such as setting.xml

 

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Category 1">
        <SwitchPreference
            android:icon="@drawable/ic_settings_24dp"
            android:key="wifi"
            android:summaryOff="disable"
            android:summaryOn="enable"
            android:title="WIFI" />
		<CheckBoxPreference
            android:icon="@drawable/ic_settings_24dp"
            android:key="wifi2"
            android:summaryOff="disable"
            android:summaryOn="enable"
            android:title="WIFI2" />
    </PreferenceCategory>
    <PreferenceCategory android:title="category two">
        <ListPreference
            android:title="Data update frequency"
            android:key="update_rate"
            android:entries="@array/update_rate"
            android:entryValues="@array/update_rate_value"
            android:summary="default" />
		<MultiSelectListPreference
            android:title="Data update frequency (multiple choices)"
            android:key="update_rate_muti"
            android:entries="@array/update_rate"
            android:entryValues="@array/update_rate_value"
            android:summary="default"/>
    </PreferenceCategory>
    <PreferenceCategory android:title="Category 3">
        <EditTextPreference
            android:dependency="wifi"
            android:key="wifi_name"
            android:summary="default"
            android:title="WIFI Name" />
        <!-- For simple display, you can not specify key -->
        <Preference
            android:title="About"
            android:summary="XXX Studio" />
        <Preference
            android:title="version number"
            android:summary="1.0"
            android:enabled="false"
            android:selectable="false"/>
    </PreferenceCategory>
	<PreferenceCategory android:title="Category 4">
        <PreferenceScreen
            android:summary="Units, data sources, other settings..."
            android:title="More settings">
            <SwitchPreference
                android:title="Other settings"
                android:summary="" />
            <CheckBoxPreference
                android:title="options"
                android:summary="" />
        </PreferenceScreen>
    </PreferenceCategory>
</PreferenceScreen>

 Note: entries in category two need to define an array in strings.xml

as follows:

 

<string-array name="update_rate">
       <item>One hour</item>
       <item>Three hours</item>
       <item>Six hours</item>
       <item>Half a day</item>
       <item>One day</item>
</string-array>
<string-array name="update_rate_value">
       <item>1</item>
       <item>3</item>
       <item>6</item>
       <item>12</item>
       <item>24</item>
</string-array>

 

 

2. Create a set of activities, be careful not to use fragments that are support packages

 

package cn.bill56.preferencesetting;

import android.preference.PreferenceFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        // don't set setcontentview
        if (savedInstanceState == null) {
            SettingFragment settingFragment = new SettingFragment();
            getFragmentManager().beginTransaction()
                    .add(android.R.id.content,settingFragment)
                    .commit();
        }
    }

    public static class SettingFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate (savedInstanceState);
            // Load the xml resource file
            addPreferencesFromResource(R.xml.setting);
        }
    }

}

 

 

 3. Run the program, you can see the effect shown in the effect diagram

After selecting some settings, we can look inside the app to see if the data is actually being saved

Open the DBMS, you can see that there is a shared_prefs folder inside the program, expand to see the files stored in the saved options, after importing into the computer, open the file, you can see the following content (the content saved is definitely inconsistent according to your own operations) :



 

 

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="wifi_name">111</string>
    <boolean name="wifi2" value="true" />
    <set name="update_rate_muti">
        <string>1</string>
        <string>6</string>
        <string>3</string>
    </set>
    <boolean name="wifi" value="true" />
</map>

 

 

Such a simple line of code can achieve such a responsible function, it seems that the outside world is not small, you must go and see more.


 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326805090&siteId=291194637