Preference 能简便地实现设置界面,且主要优点是设置的值会自动通过 SharedPreferences 方式保存下来,可直接取用。
一、简易实现
res/xml/prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="显示一排偏好">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="开关偏好1"
android:summary="这是一个开关按钮1" />
<SwitchPreference
android:key="switch_preference"
android:title="开关偏好2"
android:summary="这是一个开关按钮2" />
</PreferenceCategory>
<PreferenceCategory
android:title="基于对话框的偏好">
<EditTextPreference
android:key="edittext_preference"
android:title="文本输入偏好"
android:summary="使用一个文本框对话框"
android:dialogTitle="输入你的宠物" />
<ListPreference
android:key="list_preference"
android:title="列表偏好"
android:summary="使用一个列表对话框"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="选择一个" />
<MultiSelectListPreference
android:title="多选列表偏好"
android:key="multiSelectList_preference"
android:summary="使用一个多选列表对话框"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="选择几个" />
</PreferenceCategory>
<PreferenceCategory
android:title="启动偏好">
<PreferenceScreen
android:key="screen_preference"
android:title="屏幕"
android:summary="显示另一个偏好屏幕">
<!-- You can place more preferences here that will be shown on the next screen. -->
<CheckBoxPreference
android:key="next_screen_checkbox_preference"
android:title="开关偏好"
android:summary="另一个屏幕上的偏好" />
</PreferenceScreen>
<PreferenceScreen
android:title="意图偏好1"
android:summary="通过意图启动一个Activity">
<intent
android:targetClass="com.ttt.zhihudaily.activity.FavouriteActivity"
android:targetPackage="com.ttt.zhihudaily"/>
</PreferenceScreen>
<PreferenceScreen
android:title="意图偏好2"
android:summary="通过意图启动一个链接">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.baidu.com" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:title="偏好属性">
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="父开关"
android:summary="这是一个父开关" />
<CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:title="子开关"
android:summary="这是一个子开关" />
</PreferenceCategory>
</PreferenceScreen>
res/values/array.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="entries_list_preference">
<item>a</item>
<item>b</item>
<item>c</item>
<item>d</item>
<item>e</item>
</string-array>
<string-array name="entryvalues_list_preference">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
</resources>
PrefsFragment:
package com.ttt.zhihudaily.fragment;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import com.ttt.zhihudaily.R;
public class PrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
- Activity 中的 addPreferencesFromResource() 已经废弃,现在都推荐用 Fragment。
PrefsActivity:
package com.ttt.zhihudaily.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import com.ttt.zhihudaily.R;
import com.ttt.zhihudaily.fragment.PrefsFragment;
public class PrefsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prefs);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
* getFragmentManager()
* .beginTransaction()
* .replace(R.id.prefs_frame,new PrefsFragment())
* .commit();
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case android.R.id.home:
finish();
break;
default:
break;
}
return true;
}
}
二、获取设置值
prefs.xml 中有设置 android:key 属性,就可以通过这个 key 从 SharedPreferences 中取值。其他 Activity 或 Fragment 等都可以这样取到值。
public class PrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getActivity());
Boolean checkbox=prefs.getBoolean("checkbox_preference",false);
Toast.makeText(getActivity(), ""+checkbox, Toast.LENGTH_SHORT).show();
}
}
三、设置点击事件和值改变事件
package com.ttt.zhihudaily.fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.widget.CheckBox;
import android.widget.Toast;
import com.ttt.zhihudaily.R;
public class PrefsFragment extends PreferenceFragment
implements Preference.OnPreferenceChangeListener,Preference.OnPreferenceClickListener{
private SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
prefs=PreferenceManager.getDefaultSharedPreferences(getActivity());
CheckBoxPreference cp=(CheckBoxPreference)findPreference("checkbox_preference");
SwitchPreference sp=(SwitchPreference)findPreference("switch_preference");
cp.setOnPreferenceClickListener(this);
sp.setOnPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getActivity(), "开关按钮2的值是:"+newValue, Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onPreferenceClick(Preference preference) {
Boolean checkboxValue=prefs.getBoolean(preference.getKey(),false);
Toast.makeText(getActivity(), "开关按钮1的值是:"+checkboxValue, Toast.LENGTH_SHORT).show();
return true;
}
}
网友评论