话不多说,先上代码,随后分析。
创建一个PrefFragment
package com.example.vm510l.myapplication;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
public class PrefFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
//判断是否选择联动的复选框已经选中,只用选中才能点击下面的item
CheckBoxPreference checkBox = (CheckBoxPreference) findPreference("select_linkage");
ListPreference editBox = (ListPreference) findPreference("select_city");
editBox.setEnabled(checkBox.isChecked());
}
//当随意点击一个item的时候,触发这个方法,判断是否选中已经联动。
//但是有个缺点,当程序初始化以后,即使没有任何点击,选择联动的复选框没有选中的状态下,
//下面的item仍然可以点击,于是在上面添加了校验代码,即使没有点击的情况下先进行校验
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if ("select_linkage".equals(preference.getKey())) {
CheckBoxPreference checkBox = (CheckBoxPreference) findPreference("select_linkage");
ListPreference editBox = (ListPreference) findPreference("select_city");
editBox.setEnabled(checkBox.isChecked());
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}
随后在res路径下,创建一个名字为xml的文件夹,创建一个文件preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Settings" >
<!-- 单独一个复选框 -->
<PreferenceCategory android:title="我是CheckBox" >
<CheckBoxPreference
android:key="checkbox_preference"
android:summary="是否勾选复选框"
android:title="我是复选框" />
</PreferenceCategory>
<!-- 单独一个Switch开关 -->
<PreferenceCategory android:title="我是Switch" >
<SwitchPreference
android:key="switch_preference"
android:summary="是否打开我呢?"
android:summaryOff="关闭"
android:summaryOn="打开"
android:switchTextOff="开关被关闭了"
android:switchTextOn="开关被打开了"
android:title="开关" />
</PreferenceCategory>
<!-- 很多选项的综合 -->
<PreferenceCategory android:title="大综合" >
<EditTextPreference
android:dialogTitle="请输入你的名字"
android:key="edittext_preference"
android:summary="这是一个编辑框"
android:title="我是编辑框" />
<ListPreference
android:dialogTitle="请选择一项"
android:entries="@array/cities"
android:entryValues="@array/cities"
android:key="single_list_preference"
android:summary="这是一个单选列表"
android:title="我是单选框" />
<MultiSelectListPreference
android:dialogTitle="请选择一项"
android:entries="@array/cities"
android:entryValues="@array/cities"
android:key="multi_list_preference"
android:summary="这是一个多选列表"
android:title="我是多选框" />
<RingtonePreference
android:key="ringtone_preference"
android:ringtoneType="all"
android:showSilent="true"
android:summary="选择各种手机铃声"
android:title="铃声" />
<PreferenceScreen
android:summary="是否能打开页面"
android:title="打开页面" >
<intent
android:action="android.intent.action.VIEW"
android:data="http://www.baidu.com" />
</PreferenceScreen>
</PreferenceCategory>
<!-- 启动新的页面 -->
<PreferenceCategory android:title="启动新页面设置" >
<PreferenceScreen
android:summary="点我到达新的页面"
android:title="新开页面展示" >
<CheckBoxPreference
android:defaultValue="true"
android:key="select_me"
android:summaryOff="对不起,你没有勾选我"
android:summaryOn="是的,你勾选了我"
android:title="是否勾选了我?" />
<CheckBoxPreference
android:defaultValue="false"
android:key="like_android"
android:summaryOff="不不,我没太大兴趣"
android:summaryOn="是的,我特别喜欢"
android:title="是否喜欢Android呢?" />
</PreferenceScreen>
</PreferenceCategory>
<!-- 调试onPreferenceTreeClick接口 -->
<PreferenceCategory android:title="联动设置" >
<CheckBoxPreference
android:key="select_linkage"
android:title="是否选择联动" />
<ListPreference
android:dialogTitle="请选择一个城市"
android:entries="@array/cities"
android:entryValues="@array/cities"
android:key="select_city"
android:summary="我是否能选择看联动设置"
android:title="是否能选择" />
</PreferenceCategory>
</PreferenceScreen>
随后创建一个Activity,代码如下:
package com.example.vm510l.myapplication;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加载PrefFragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
PrefFragment prefFragment = new PrefFragment();
//transaction.add(R.id.prefFragment, prefFragment);
//add和replace都可以
transaction.replace(R.id.prefFragment, prefFragment);
transaction.commit();
}
}
再下面是Activity的布局,和preference文件里面的array/cities
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/prefFragment"
>
</FrameLayout>
</LinearLayout>
<resources>
<string name="app_name">My Application</string>
<string-array name="cities">
<item>上海</item>
<item>北京</item>
<item>广州</item>
<item>深圳</item>
<item>杭州</item>
</string-array>
</resources>
无法上图,可能是我电脑的问题。但是不影响运行。