Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 安卓系統設置選項的框架。,安卓系統設置框架

安卓系統設置選項的框架。,安卓系統設置框架

編輯:關於android開發

安卓系統設置選項的框架。,安卓系統設置框架


首先在res文件中創建一個xml文件夾,創建preference.xml文件,然後寫布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
 3 
 4     <PreferenceCategory android:title="系統信息" >
 5         <EditTextPreference
 6             android:defaultValue="機主姓名"
 7             android:icon="@drawable/ic_launcher"
 8             android:key="edit_text_key_1"
 9             android:summary="木子小新"
10             android:title="機主姓名" />
11 
12         <ListPreference
13             android:dialogIcon="@drawable/ic_launcher"
14             android:dialogTitle="城市區號"
15             android:entries="@array/city"
16             android:entryValues="@array/city_quhao"
17             android:icon="@drawable/ic_launcher"
18             android:key="listpreference"
19             android:title="區號" />
20     </PreferenceCategory>
21     <PreferenceCategory android:title="系統設置" >
22         <Preference
23             android:icon="@drawable/bokeyuan"
24             android:summary="http://www.cnblogs.com/labixiaoxin/"
25             android:title="機主博客" >
26             <intent
27                 android:action="android.intent.action.VIEW"
28                 android:data="http://www.cnblogs.com/labixiaoxin/" />
29         </Preference>
30 
31         <SwitchPreference
32             android:icon="@drawable/wifi"
33             android:key="wifi"
34             android:title="wifi開關" />
35         <SwitchPreference
36             android:icon="@drawable/lanya"
37             android:key="lanya"
38             android:title="藍牙開關" />
39 
40         <CheckBoxPreference
41             android:defaultValue="true"
42             android:icon="@drawable/xuanzhuahn"
43             android:key="checkbox_key_xuanzhuan"
44             android:title="旋轉" />
45 
46         <RingtonePreference
47             android:icon="@drawable/ring"
48             android:key="ring"
49             android:title="鈴聲" />
50     </PreferenceCategory>
51 
52     <PreferenceScreen
53         android:icon="@drawable/ic_launcher"
54         android:key="preference_screen_2_key"
55         android:persistent="false"
56         android:title="其他設置" >
57         <CheckBoxPreference
58             android:defaultValue="true"
59             android:icon="@drawable/ic_launcher"
60             android:key="checkbox_key_wangluo"
61             android:summary="網絡已經開啟"
62             android:title="網絡" />
63     </PreferenceScreen>
64 
65 </PreferenceScreen>


從主Acitivity中加載SettingFragment:

 1 package com.lixu.android_set;
 2 
 3 import android.app.Activity;
 4 import android.app.FragmentManager;
 5 import android.app.FragmentTransaction;
 6 import android.os.Bundle;
 7 
 8 public class MainActivity extends Activity {
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         // 創建Fragment管理器
14         FragmentManager fm = this.getFragmentManager();
15         FragmentTransaction ft = fm.beginTransaction();
16         // 將原有的Activity替換成Fragment。也可以添加到原有之上不覆蓋 如:ft.add(R.id.rel, new
17         // Fragment()) ;如果添加的多了會比較耗資源。
18         ft.replace(android.R.id.content, new SettingFragment());
19         // 提交
20         ft.commit();
21 
22     }
23 }
  1 package com.lixu.android_set;
  2 
  3 import android.content.SharedPreferences;
  4 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  5 import android.os.Bundle;
  6 import android.preference.CheckBoxPreference;
  7 import android.preference.EditTextPreference;
  8 import android.preference.ListPreference;
  9 import android.preference.PreferenceFragment;
 10 import android.preference.PreferenceManager;
 11 import android.preference.SwitchPreference;
 12 
 13 //安卓官方申明在3.0版本後最好不要用FragmentActivity 應該用PreferenceFragment,在從主Activity中調用。
 14 public class SettingFragment extends PreferenceFragment {
 15 
 16     private MyListener mMyListener = new MyListener();
 17     private EditTextPreference mEditTextPreference;
 18     private ListPreference mListPreference;
 19     private CheckBoxPreference mCheckBoxPreference;
 20     private SwitchPreference mSwitchPreference1;
 21     private SwitchPreference mSwitchPreference2;
 22 
 23     @Override
 24     public void onCreate(Bundle savedInstanceState) {
 25         super.onCreate(savedInstanceState);
 26         // 讀取值的通用方法
 27         SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
 28 
 29         // 添加設置選項
 30         addPreferencesFromResource(R.xml.preferences);
 31         // 找到各個控件按鈕
 32         mEditTextPreference = (EditTextPreference) findPreference("edit_text_key_1");
 33 
 34         mListPreference = (ListPreference) findPreference("listpreference");
 35 
 36         mCheckBoxPreference = (CheckBoxPreference) findPreference("checkbox_key_xuanzhuan");
 37 
 38         mSwitchPreference1 = (SwitchPreference) findPreference("wifi");
 39         mSwitchPreference2 = (SwitchPreference) findPreference("lanya");
 40         // 根據實際情況設置提示框的初始狀態
 41         boolean s1 = sharedpreferences.getBoolean("wifi", false);
 42         if (!s1)
 43             mSwitchPreference1.setSummary("wifi關閉");
 44         else if (s1)
 45             mSwitchPreference1.setSummary("wifi開啟");
 46 
 47         boolean s2 = sharedpreferences.getBoolean("lanya", false);
 48         if (!s2)
 49             mSwitchPreference2.setSummary("藍牙關閉");
 50         else if (s2)
 51             mSwitchPreference2.setSummary("藍牙開啟");
 52 
 53         boolean s = sharedpreferences.getBoolean("checkbox_key_xuanzhuan", false);
 54         if (!s)
 55             mCheckBoxPreference.setSummary("旋轉關閉");
 56         else if (s)
 57             mCheckBoxPreference.setSummary("旋轉開啟");
 58 
 59         // 添加項目數據發生變化時候的監聽
 60         sharedpreferences.unregisterOnSharedPreferenceChangeListener(mMyListener);
 61     }
 62 
 63     // 程序暫停時取消注冊監聽事件,使得代碼更加完整。
 64     @Override
 65     public void onPause() {
 66         super.onPause();
 67         getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(mMyListener);
 68     }
 69 
 70     // 程序運行時注冊
 71     @Override
 72     public void onResume() {
 73         super.onResume();
 74         getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(mMyListener);
 75     }
 76 
 77     private class MyListener implements OnSharedPreferenceChangeListener {
 78 
 79         @Override
 80         public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
 81 
 82             switch (key) {
 83             case "edit_text_key_1":
 84                 String str1 = sharedPreferences.getString("edit_text_key_1", "null");
 85                 mEditTextPreference.setSummary(str1);
 86 
 87                 break;
 88             case "listpreference":
 89                 String city = mListPreference.getValue();
 90                 String city_num = mListPreference.getEntry() + "";
 91                 mListPreference.setSummary(city + " " + city_num);
 92 
 93                 break;
 94             case "checkbox_key_xuanzhuan":
 95                 boolean s = sharedPreferences.getBoolean("checkbox_key_xuanzhuan", false);
 96                 if (!s)
 97                     mCheckBoxPreference.setSummary("旋轉關閉");
 98                 else if (s)
 99                     mCheckBoxPreference.setSummary("旋轉開啟");
100 
101                 break;
102             case "wifi":
103                 boolean s1 = sharedPreferences.getBoolean("wifi", false);
104                 if (!s1)
105                     mSwitchPreference1.setSummary("wifi關閉");
106                 else if (s1)
107                     mSwitchPreference1.setSummary("wifi開啟");
108 
109                 break;
110             case "lanya":
111 
112                 boolean s2 = sharedPreferences.getBoolean("lanya", false);
113                 if (!s2)
114                     mSwitchPreference2.setSummary("藍牙關閉");
115                 else if (s2)
116                     mSwitchPreference2.setSummary("藍牙開啟");
117 
118                 break;
119 
120             default:
121                 break;
122             }
123         }
124     }
125 
126 }
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <string-array name="city">
 5         <item>成都</item>
 6         <item>北京</item>
 7         <item>上海</item>
 8     </string-array>
 9     <string-array name="city_quhao">
10         <item>028</item>
11         <item>010</item>
12         <item>021</item>
13     </string-array>
14 
15 </resources>

運行效果圖:

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved