Perferenceactivity系統組建可以實現系統設置的窗體
Perferenceactivity系統組建可以實現系統設置的窗體,同時省去了我們自己去寫配置文件
這里面類型很多 下面上代碼 自己看效果 自己看下代碼就可以懂 很簡單
首先在res中創建一個文件夾xml,里面建立一個文件setting.xml代表perferenceactivity的關聯文件
setting.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 3 4 <CheckBoxPreference 5 android:key="setting_key" 6 android:summary="我是默認的描述" 7 android:summaryOff="我沒有被勾選" 8 android:summaryOn="我被勾選了" 9 android:title="我是標題" /> 10 11 <PreferenceCategory android:title="我是小標題" > 12 <CheckBoxPreference 13 android:key="download_pic" 14 android:summary="設置2g/3g的網絡請求" 15 android:summaryOff="2g/3g禁止下載圖片" 16 android:summaryOn="2g/3g下載圖片" 17 android:title="手機網絡下載設置" /> 18 </PreferenceCategory> 19 20 <EditTextPreference 21 android:dialogTitle="dialogtitle" 22 android:key="dialog_key" 23 android:negativeButtonText="取消" 24 android:positiveButtonText="確定" 25 android:summary="對話框summary" 26 android:title="dialog_title" /> 27 28 <ListPreference 29 android:dialogTitle="選擇" 30 android:entries="@array/name" 31 android:entryValues="@array/value" 32 android:key="list_key" 33 android:positiveButtonText="確定" 34 android:summary="概要" 35 android:title="標題" /> 36 37 </PreferenceScreen>
當是listPreferenceScreen時引入一個在values文件夾下的string.xml文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="hello">Hello World, DemoActivity!</string> 5 <string name="app_name">PreferenceActivity</string> 6 <string-array name="name"> 7 <item >name1</item> 8 <item >name2</item> 9 <item >name3</item> 10 <item >name4</item> 11 <item >name5</item> 12 </string-array> 13 <string-array name="value"> 14 <item >value1</item> 15 <item >value2</item> 16 <item >value3</item> 17 <item >value4</item> 18 <item >value5</item> 19 </string-array> 20 </resources>
下面這個是主界面加載的xml 很簡單 就是做跳轉界面使用的沒其他多余作用
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/hello" /> 11 12 <Button 13 android:id="@+id/button1" 14 android:onClick="click" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="打開設置界面" /> 18 19 </LinearLayout>
DemoActivity.java
1 package cn.itcast.preferencedemo; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.content.SharedPreferences; 6 import android.os.Bundle; 7 import android.preference.PreferenceManager; 8 import android.view.View; 9 import android.widget.Toast; 10 11 public class DemoActivity extends Activity { 12 /** Called when the activity is first created. */ 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.main); 17 18 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 19 String value = sp.getString("list_key", ""); 20 Toast.makeText(this, value, 0).show(); 21 22 } 23 24 public void click(View view){ 25 Intent intent = new Intent(this,SettingActivity.class); 26 startActivity(intent); 27 } 28 }
SettingActivity.java
1 package cn.itcast.preferencedemo; 2 3 import android.os.Bundle; 4 import android.preference.Preference; 5 import android.preference.PreferenceActivity; 6 import android.preference.PreferenceScreen; 7 8 public class SettingActivity extends PreferenceActivity { 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 13 super.onCreate(savedInstanceState); 14 addPreferencesFromResource(R.xml.setting); 15 16 } 17 18 }
如果要拿里面的數據 就通過getPerferenceManager.getDefuletPerference去獲取

浙公網安備 33010602011771號