注释:部分代码引用http:
单一控件:
Preference
CheckPreference
EditTextPreference
ListPreference
RingtonePreference
组合控件:
PreferenceCategory :
类似于LinearLayou、RelativeLayout,用于组合一组Preference,使布局更具备层次感 。
PreferenceScreen : 所有Preference元素的根节点。
**android
3.0以前使用方法**
使我们的Activity继承PreferenceActivity,然后在onCreate()方法中通过
addPreferencesFromResource(R.xml.custom_preference) (我们自定义的Preference 布局)。
3.0以后已经不建议使用了 推荐使用PreferenceFragment 在onCreate()方法中
addPreferencesFromResource(R.xml.preference); 在Activity中引入Freagment
代码如下 :
在res目录下新建xml文件夹 新建preference.xml文件
<?xml version=
"1.0" encoding=
"utf-8"?>
<PreferenceScreen xmlns:android=
"http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key=
"set_local"
android:title=
"我的位置" />
<CheckBoxPreference
android:defaultValue=
"true"
android:key=
"apply_wireless"
android:summary=
"使用无线网络在应用程序(例如Google地图)中查看位置"
android:title=
"使用无线网络"></CheckBoxPreference>
<CheckBoxPreference
android:key=
"apply_gps"
android:summary=
"定位到街道级别(需要消耗更多的电量以及天气允许)"
android:title=
"使用GPS"></CheckBoxPreference>
<PreferenceCategory android:title=
"无线和网络设置"></PreferenceCategory>
<CheckBoxPreference
android:key=
"apply_fly"
android:summary=
"禁用所有无线连接"
android:title=
"飞行模式"></CheckBoxPreference>
<CheckBoxPreference
android:key=
"apply_internet"
android:summary=
"禁用通过USB共享Internet连接"
android:title=
"Internet共享"></CheckBoxPreference>
<CheckBoxPreference
android:key=
"apply_wifi"
android:summary=
"打开Wi-Fi"
android:title=
"Wi-Fi"></CheckBoxPreference>
<Preference
android:dependency=
"apply_wifi"
android:key=
"wifi_setting"
android:summary=
"设置和管理无线接入点"
android:title=
"Wi-Fi设置">
<!-- 对应textView 点击时 自定义一个默认跳转Intent action指定隐式Intent -->
<!-- action指定隐式Intent ; targetPackage和targetClass指定显示Intent-->
<intent
android:action=
"com.feixun.action.seemAction"
android:targetClass=
"com.example.preference.SecondActivity"
android:targetPackage=
"com.example.preference" />
</Preference>
<CheckBoxPreference
android:key=
"apply_bluetooth"
android:summary=
"启用蓝牙"
android:title=
"蓝牙"></CheckBoxPreference>
<Preference
android:dependency=
"apply_bluetooth"
android:key=
"bluetooth_setting"
android:summary=
"管理连接、设备设备名称和可检测性"
android:title=
"蓝牙设置"></Preference>
<EditTextPreference
android:defaultValue=
"123"
android:key=
"number_edit"
android:title=
"输入电话号码"></EditTextPreference>
<ListPreference
android:dialogTitle=
"选择部门"
android:entries=
"@array/ceshi1"
android:entryValues=
"@array/ceshi1_value"
android:key=
"depart_value"
android:title=
"部门设置"></ListPreference>
<RingtonePreference
android:key=
"ring_key"
android:ringtoneType=
"all"
android:showDefault=
"true"
android:showSilent=
"true"
android:title=
"铃声"></RingtonePreference>
</PreferenceScreen>
**frengment中代码**
public class SpFrengment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {
private CheckBoxPreference mapply_wifiPreference;
private CheckBoxPreference mapply_internetPreference;
private ListPreference depart_valuePreference;
private EditTextPreference number_editPreference;
private Preference mwifi_settingPreference;
private CheckBoxPreference apply_wireless;
private CheckBoxPreference apply_gps;
private CheckBoxPreference apply_bluetooth;
private Preference bluetooth_setting;
private RingtonePreference ring_key;
private String oldDeptId;
public SpFrengment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
**
mapply_wifiPreference = (CheckBoxPreference) findPreference(
"apply_wifi");
mapply_internetPreference = (CheckBoxPreference) findPreference(
"apply_internet");
depart_valuePreference = (ListPreference) findPreference(
"depart_value");
number_editPreference = (EditTextPreference) findPreference(
"number_edit");
mwifi_settingPreference = (Preference) findPreference(
"wifi_setting");
apply_wireless = (CheckBoxPreference) findPreference(
"apply_wireless");
apply_gps = (CheckBoxPreference) findPreference(
"apply_gps");
apply_bluetooth = (CheckBoxPreference) findPreference(
"apply_bluetooth");
bluetooth_setting = (Preference) findPreference(
"bluetooth_setting");
ring_key = (RingtonePreference) findPreference(
"ring_key");
**
mapply_internetPreference.setOnPreferenceChangeListener(
this);
mapply_internetPreference.setOnPreferenceChangeListener(
this);
depart_valuePreference.setOnPreferenceChangeListener(
this);
number_editPreference.setOnPreferenceChangeListener(
this);
mwifi_settingPreference.setOnPreferenceChangeListener(
this);
apply_wireless.setOnPreferenceChangeListener(
this);
apply_gps.setOnPreferenceChangeListener(
this);
apply_bluetooth.setOnPreferenceChangeListener(
this);
bluetooth_setting.setOnPreferenceChangeListener(
this);
ring_key.setOnPreferenceChangeListener(
this);
mapply_wifiPreference.setOnPreferenceChangeListener(
this);
**得到我们的存储Preferences值的对象,然后对其进行相应操作**
SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean apply_wifiChecked = shp.getBoolean(
"apply_wifi",
false);
}
@Override
public boolean onPreferenceChange(Preference preference, Object objValue) {
if (preference == mapply_wifiPreference) {
Toast.makeText(getActivity(),
"mapply_wifiPreference", Toast.LENGTH_SHORT).show();
}
else if (preference.getKey().equals(
"apply_internet")) {
Toast.makeText(getActivity(),
"apply_internet", Toast.LENGTH_SHORT).show();
}
else if (preference == mapply_internetPreference) {
Toast.makeText(getActivity(),
"mapply_internetPreference", Toast.LENGTH_SHORT).show();
}
else if (preference.getKey().equals(
"wifi_setting")) {
Toast.makeText(getActivity(),
"wifi_setting", Toast.LENGTH_SHORT).show();
}
else if (preference == depart_valuePreference) {
Toast.makeText(getActivity(),
"depart_valuePreference", Toast.LENGTH_SHORT).show();
}
else if (preference == number_editPreference) {
Toast.makeText(getActivity(),
"number_editPreference", Toast.LENGTH_SHORT).show();
}
else if (preference == mwifi_settingPreference) {
Toast.makeText(getActivity(),
"mwifi_settingPreference", Toast.LENGTH_SHORT).show();
}
else if (preference == apply_wireless) {
Toast.makeText(getActivity(),
"apply_wireless", Toast.LENGTH_SHORT).show();
}
else if (preference == apply_gps) {
Toast.makeText(getActivity(),
"apply_gps", Toast.LENGTH_SHORT).show();
}
else if (preference == apply_bluetooth) {
Toast.makeText(getActivity(),
"apply_bluetooth", Toast.LENGTH_SHORT).show();
}
else if (preference == bluetooth_setting) {
Toast.makeText(getActivity(),
"bluetooth_setting", Toast.LENGTH_SHORT).show();
}
else if (preference == ring_key) {
Toast.makeText(getActivity(),
"ring_key", Toast.LENGTH_SHORT).show();
}
return true;
}
}
**Activity中**
public class SpActivity extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_spactivity);
getFragmentManager().beginTransaction().replace(R.id.frent,
new SpFrengment()).commit();
}
}