新建工程Android_data_settings選中API11:Android3.0(HoneyComb)
在res新建文件夾xml中創建mysetting.xml文件:New Android XML File->Resource Type 中選中Preference,File命名爲mysettings.xml點擊next->finsh
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 設置的類別 -->
<PreferenceCategory
android:key="mylocation"
android:summary="我的位置"
android:title="我的位置源" >
<CheckBoxPreference
android:key="wireless_network"
android:summary="使用無線網絡查看應用程序(例如Google地圖)中的位置"
android:title="使用無線網絡" />
<CheckBoxPreference
android:key="gps_satellite_setting"
android:summary="定位時,精確到街道級別(取消選擇可節約電量)"
android:title="啓用GPS衛星設置" />
</PreferenceCategory>
<PreferenceCategory
android:key="mymessage"
android:summary="個人信息設置"
android:title="個人信息設置" >
<CheckBoxPreference
android:key="yesno_save_individual_info"
android:title="是否保存個人信息" />
<EditTextPreference
android:key="individual_name"
android:summary="請輸入真實姓名"
android:title="姓名 " />
<ListPreference
android:entries="@array/citys"
android:entryValues="@array/citys"
android:key="mycity"
android:summary="所屬城市"
android:title="所屬城市" />
</PreferenceCategory>
</PreferenceScreen>
strings.xml代碼
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Android_data_settings</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="citys">
<item>北京</item>
<item>上海</item>
<item>廣州</item>
</string-array>
</resources>
MainActivity.java代碼
package com.example.android_data_settings;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends PreferenceActivity implements OnPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName("mysettings");
addPreferencesFromResource(R.xml.mysettings);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPreferenceChange(Preference arg0, Object arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
// TODO Auto-generated method stub
if ("yesno_save_individual_info".equals(preference.getKey())) {
Toast.makeText(MainActivity.this, "hello", 1).show();
CheckBoxPreference checkBoxPreference = (CheckBoxPreference) findPreference("yesno_save_individual_info");
EditTextPreference editTextPreference = (EditTextPreference) findPreference("individual_name");
editTextPreference.setEnabled(checkBoxPreference.isChecked());
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}
當勾選了是否保存個人信息,下面的姓名一欄就可以輸入。