PreferenceActivity使用方法
1 public class MainActivity extends Activity { 2 @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 setContentView(R.layout.activity_main); 6 7 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { 8 9 @Override 10 public void onClick(View v) { 11 // TODO Auto-generated method stub 12 startActivity(new Intent(getApplicationContext(), MyPreferenceActivity.class)); 13 } 14 }); 15 } 16 }
1 package com.example.preferenceactivity; 2 3 import android.os.Bundle; 4 import android.preference.CheckBoxPreference; 5 import android.preference.EditTextPreference; 6 import android.preference.ListPreference; 7 import android.preference.PreferenceActivity; 8 import android.preference.PreferenceManager; 9 import android.widget.Toast; 10 11 /** 12 * 1. CheckBoxPreference. 13 * 2. ListPreference. 14 * 3. editTextPreference. 15 * 16 * @author dr 17 */ 18 public class MyPreferenceActivity extends PreferenceActivity { 19 20 PreferenceManager manager; 21 CheckBoxPreference checkBoxPreference; 22 ListPreference listPreference; 23 EditTextPreference editTextPreference; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 // TODO Auto-generated method stub 28 super.onCreate(savedInstanceState); 29 30 addPreferencesFromResource(R.xml.mypreferenc); 31 32 manager = getPreferenceManager(); 33 checkBoxPreference = (CheckBoxPreference) manager 34 .findPreference("checkbox"); 35 Toast.makeText(getApplicationContext(), 36 "当前的状态为:" + checkBoxPreference.isChecked(), Toast.LENGTH_SHORT) 37 .show(); 38 39 listPreference = (ListPreference) manager.findPreference("list"); 40 Toast.makeText( 41 getApplicationContext(), 42 listPreference.getEntry() + "的开发环境为:" 43 + listPreference.getValue(), Toast.LENGTH_SHORT).show(); 44 45 editTextPreference = (EditTextPreference) manager 46 .findPreference("text"); 47 Toast.makeText(getApplicationContext(), 48 "您输入的内容为:" + editTextPreference.getText(), Toast.LENGTH_SHORT) 49 .show(); 50 51 } 52 }
在res目录下面,新建一个xml文件,里面 mypreferenc.xml。
1 <?xml version="1.0" encoding="utf-8"?> 2 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 3 <CheckBoxPreference 4 android:key="checkbox" 5 android:title="是否开启" 6 android:summaryOn="已经开启" 7 android:summaryOff="已经关闭" /> 8 9 <ListPreference 10 android:key="list" 11 android:title="选择第一个选项" 12 android:summary="请点击选择" 13 android:entries="@array/entries" 14 android:entryValues="@array/values" /> 15 16 <EditTextPreference 17 android:key="text" 18 android:dialogTitle="请输入你的名字" 19 android:dialogMessage="有劳了" 20 android:summary="请在此输入" 21 android:title="请输入" /> 22 23 </PreferenceScreen>
在res/values,里面 mylistpreference.xml。
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string-array name="entries"> 5 <item>Java</item> 6 <item>Swift</item> 7 <item>C#</item> 8 </string-array> 9 10 <string-array name="values"> 11 <item>Eclips</item> 12 <item>Xcode</item> 13 <item>Visual Studio</item> 14 </string-array> 15 16 </resources>