Android 安卓技术分享Android-杂章Android 开发技术分享

这才是Android设置界面的正确做法👌👌👌

2017-02-26  本文已影响1093人  Android开发哥

效果图

效果图

其实这种选项的界面,不需要自己去一个一个选项地去做。完后还要自己去保存设置,保存了还要读取真的是哔了狗了。
还好Android为我们提供了一些封装好的东西。那就是PreferenceActivityPreferenceFragment。本文将会使用PreferenceFragment作为范例。

PreferenceFragment

从名字中不难看出,PreferenceFragmentFragment的一个子类,所以大致使用上是差不多的,除了一些特性之外。

使用方式

有两种加载选项的的方式:

几个概念

支持类型

使用

首先我们新建两份数组资源在

/res/values

wifi.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="wifi_entities">
        <item>WIFI1</item>
        <item>WIFI2</item>
        <item>WIFI3</item>
        <item>WIFI4</item>
        <item>WIFI5</item>
        <item>WIFI6</item>
    </string-array>
    <string-array name="wifi_values">
        <item>无线网络1</item>
        <item>无线网络2</item>
        <item>无线网络3</item>
        <item>无线网络4</item>
        <item>无线网络5</item>
        <item>无线网络6</item>
    </string-array>
</resources>

type.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="type_entities">
        <item>TYPE1</item>
        <item>TYPE2</item>
        <item>TYPE3</item>
        <item>TYPE4</item>
        <item>TYPE5</item>
    </string-array>
    <string-array name="type_values">
        <item>类型1</item>
        <item>类型2</item>
        <item>类型3</item>
        <item>类型4</item>
        <item>类型5</item>
    </string-array>
</resources>

这里就先不说为什么每个数组文件里面要有两个数组。看下面。。。

新建一个自定义布局文件(可选)

/res/layout下新建text_view.xml

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

</TextView>

新建Preference资源文件

/res下面新建xml目录

preference_demo.xml

/res/xml下新建preference_demo.xml文件

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="普通选项">
        <Preference
            android:key="option11"
            android:summary="这是选项1"
            android:title="点我" />
        <Preference
            android:key="option111"
            android:layout="@layout/text_view" />


        <EditTextPreference
            android:key="option12"
            android:summary="这是选项2"
            android:title="名称" />

        <SwitchPreference
            android:checked="true"
            android:key="option13"
            android:summary="这是选项3"
            android:summaryOff="关闭选项3"
            android:summaryOn="开启选项3"
            android:title="通知栏常驻" />

        <RingtonePreference
            android:key="option14"
            android:summary="这是选项4"
            android:title="打开" />
        <CheckBoxPreference
            android:key="option15"
            android:layout="?android:attr/preferenceLayoutChild"
            android:summary="QQ选项"
            android:summaryOff="禁止QQ"
            android:summaryOn="允许QQ"
            android:title="QQ弹窗" />
        <CheckBoxPreference
            android:dependency="option15"
            android:key="option20"
            android:summary="微信选项"
            android:summaryOff="禁止微信"
            android:summaryOn="允许微信"
            android:title="微信弹窗" />
        <CheckBoxPreference
            android:checked="true"
            android:key="option16"
            android:summary="微博选项"
            android:summaryOff="禁止微博"
            android:summaryOn="允许我播"
            android:title="微博弹窗" />
    </PreferenceCategory>
    <PreferenceCategory android:title="复合选项">

        <ListPreference
            android:dialogTitle="对话框标题"
            android:entries="@array/wifi_entities"
            android:entryValues="@array/wifi_values"
            android:key="option17"
            android:summary="请选择WIFI"
            android:title="WIFI" />


        <MultiSelectListPreference
            android:dialogIcon="@mipmap/ic_launcher"
            android:entries="@array/type_entities"
            android:entryValues="@array/type_values"
            android:key="option18"
            android:title="消息类型" />

        <PreferenceScreen
            android:key="option19"
            android:summary="打开我的博客"
            android:title="我的博客">

            <intent
                android:action="android.intent.action.VIEW"
                android:data="http://august1996.top" />
        </PreferenceScreen>
    </PreferenceCategory>
</PreferenceScreen>

PreferenceScreen的另一个用法就是和Intent结合使用
有一些属性还是需要说明一下的

针对ListPreference和MultiSelectListPreference的属性

因此上面定义的数组资源中,两个数组的长度必须相等

代码

public class PreferenceFrag extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference_demo);
    }
}

此处不需要setContentView,而是直接addPreferencesFromResource从资源中去加载。

更多交互

Preference mOption1 = findPreference("option1");
mOption1.setOnPreferenceClickListener(this);
mOption1.setOnPreferenceChangeListener(this);

第一行我们通过键值去查找对应的Preference
第二行我们去设置对应的Preference的点击事件
第三行我们去设置对应的Preference的值改变时的监听事件

上一篇 下一篇

猜你喜欢

热点阅读