在Android App都有像是"設定"的選單,讓使用者去客制自己的選項
這個時候就可以使用Preference來設計你的選單
res/xml/setting
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/wifi_setting" >
<CheckBoxPreference
android:key="@string/test1_key"
android:defaultValue="true"
android:title="@string/title1"
android:summary="@string/summary1" />
<CheckBoxPreference
android:key="@string/test2_key"
android:dependency="@string/test1_key"
android:defaultValue="false"
android:title="@string/title2"
android:summary="@string/summary2" />
<ListPreference
android:key="@string/test3_key"
android:dependency="@string/test1_key"
android:entries="@array/interval"
android:entryValues="@array/intervalValues"
android:defaultValue="30"
android:title="@string/title3"
android:summary="@string/summary3" />
</PreferenceCategory>
</PreferenceScreen>
我們有時候會看到當選擇了某一項開關,下面的選擇都會被關閉不可選,就是用dependency來指定
只要去指定要follow的key就可以
上面的interval,intervalValues的值就設定在arrary.xml也就是下面
res/value/array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="wifi_update_interval">
<item name="10">10 秒鐘</item>
<item name="30">30 秒鐘</item>
<item name="60">1 分鐘</item>
<item name="300">5 分鐘</item>
</string-array>
<string-array name="wifi_update_intervalValues">
<item name="10">10</item>
<item name="30">30</item>
<item name="60">60</item>
<item name="300">300</item>
</string-array>
</resources>
接著建立一個Option Class
public class option extends PreferenceActivity implements OnPreferenceChangeListener {
/*** Debugging ***/
private static final String TAG = "option";
private static final boolean D = true;
String test1_key, test2_key, test3_key;
CheckBoxPreference test1, test2;
ListPreference test3;
@Override
@SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.setting);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
test1_key = getResources().getString(R.string.test1_key);
test2_key = getResources().getString(R.string.test2_key);
test3_key = getResources().getString(R.string.test3_key);
test1 = (CheckBoxPreference) findPreference(test1_key);
test2 = (CheckBoxPreference) findPreference(test2_key);
test3 = (ListPreference) findPreference(test3_key);
test1.setOnPreferenceChangeListener(this);
test2.setOnPreferenceChangeListener(this);
test3.setOnPreferenceChangeListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO Auto-generated method stub
if(D) Log.e(TAG, "Change preference.key: " preference.getKey().toString());
if(D) Log.e(TAG, "Change preference.key: " newValue.toString());
if(preference.getKey().equals(test1_key)) {
} else if(preference.getKey().equals(test2_key)) {
} else if(preference.getKey().equals(test3_key)) {
}
return true;
}
}
onPreferenceChange這個Listener會去Listener當值變化的時候,你可以將你的值紀錄起來,這樣大致上就完成
但這個還是有一個問題,你的系統已經紀錄你的option的變化,但你的所設定的defValue卻沒有相同
這時你還需要加入下面來讓你的defValue一樣
public class MainActivity extends Activity {
/*** Debugging ***/
private static final String TAG = "MainActivity";
private static final boolean D = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(D) Log.e(TAG, "item.getItemId() " item.getItemId());
switch(item.getItemId()) {
case R.id.menu_settings:
startActivity(new Intent().setClass(this , option.class));
break;
}
return false;
}
private void init_option() {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mContext);
test1 = pref.getBoolean("test1_key", true);
test2 = pref.getBoolean("test2_key", false);
test3 = Integer.parseInt(pref.getString("test3_key", "30"));
}
}