Skip to content

he476/preferenceTest

Repository files navigation

preferenceTest

使用PreferenceFragment实现设置页面

image

实现效果

image

使用 XML 文件定义设置的集合:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    ...
    ...
</PreferenceScreen>

CheckBoxPreference的实现

<PreferenceCategory
        android:title="In-Line preference"
        android:key="pc_1">

        <CheckBoxPreference
            android:key="pref_cb"
            android:title="Checkbox preference"
            android:summary="this is a checkbox"
            android:defaultValue="true" />

</PreferenceCategory>

Dialog-based preferences:

EditTextPreference
ListPreference

<PreferenceCategory
        android:title="Dialog-based preference"
        android:key="pc_2">
        <EditTextPreference
            android:key="pref_et"
            android:title="Edit Text preference"
            android:summary="an example that uses an edit text diglog"
            />
        <ListPreference
            android:key="pref_list"
            android:title="List preference"
            android:summary="an example that uses a list dialog"
            android:entries="@array/choose"
            android:entryValues="@array/chooseValue"
            android:dialogTitle="Choose one">
        </ListPreference>

    </PreferenceCategory>

ListPreference的键值对设置:

<string-array name="choose">
        <item>Alpha Option 01 </item>
        <item>Beta Option 02</item>
        <item>Charlie Option 03</item>
    </string-array>
    <string-array name="chooseValue">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>

实验效果:

image
image

Launch preferences

PreferenceScreen: 跳转到另一个PreferenceScreen

<PreferenceScreen
            android:key="pref_screen"
            android:persistent="false"
            android:summary="shows another screen of preferences"
            android:title="Screen preference">
            <CheckBoxPreference
                android:key="pref_cb2"
                android:summary="@string/pref_cb2_summary" />
        </PreferenceScreen>

实验效果:

image

PreferenceScreen: 启动一个网页

<Preference
            android:summary="@string/pref_intent_summary"
            android:title="Intent preference">
            <intent
                android:action="android.intent.action.VIEW"
                android:data="https://github.com/he476" />
        </Preference>

Preference attributes

CheckBox: 父选项 CheckBox: 子选项,当父选项勾选时呈现

<PreferenceCategory
        android:key="pc_4"
        android:title="Preference attribute">
        <CheckBoxPreference
            android:key="pref_cb3"
            android:title="Parent checkbox preference"
            android:summary="this is visually a parent"
            android:defaultValue="false"
            />
        <CheckBoxPreference
            android:key="pref_cb4"
            android:title="Child checkbox preference"
            android:summary="this is visually a child"
            android:dependency="pref_cb3"
            />
    </PreferenceCategory>

image

image

About

使用PreferenceFragment实现设置页面

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages