Framework for setting options in Android ., Android framework

Source: Internet
Author: User

Framework for setting options in Android ., Android framework

First, create an xml folder in the res file, create the preference. xml file, and then write the layout:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <PreferenceScreen xmlns: android = "http://schemas.android.com/apk/res/android"> 3 4 <PreferenceCategory android: title = "system information"> 5 <EditTextPreference 6 android: defaultValue = "Host Name" 7 android: icon = "@ drawable/ic_launcher" 8 android: key = "edit_text_key_1" 9 android: summary = "Mu Zi xiaoxin" 10 android: title = "Host Name"/> 11 12 <ListPreference13 android: dialogIcon = "@ drawable/ic_launcher" 14 android: dialogTitle = "city code" 15 android: entries = "@ array/city" 16 android: entryValues = "@ array/city_quhao" 17 android: icon = "@ drawable/ic_launcher" 18 android: key = "listpreference" 19 android: title = "area code"/> 20 </PreferenceCategory> 21 <PreferenceCategory android: title = "system settings"> 22 <Preference23 android: icon = "@ drawable/bokeyuan" 24 android: summary = "http://www.cnblogs.com/labixiaoxin/" 25 android: title = "host blog"> 26 <intent27 android: action = "android. intent. action. VIEW "28 android: data =" http://www.cnblogs.com/labixiaoxin/ "/> 29 </Preference> 30 31 <SwitchPreference32 android: icon =" @ drawable/wifi "33 android: key = "wifi" 34 android: title = "wifi switch"/> 35 <SwitchPreference36 android: icon = "@ drawable/lanya" 37 android: key = "lanya" 38 android: title = "Bluetooth switch"/> 39 40 <CheckBoxPreference41 android: defaultValue = "true" 42 android: icon = "@ drawable/xuanzhuahn" 43 android: key = "checkbox_key_xuanzhuan" 44 android: title = "Rotate"/> 45 46 <RingtonePreference47 android: icon = "@ drawable/ring" 48 android: key = "ring" 49 android: title = "ringtone"/> 50 </PreferenceCategory> 51 52 <PreferenceScreen53 android: icon = "@ drawable/ic_launcher" 54 android: key = "preference_screen_2_key" 55 android: persistent = "false" 56 android: title = "other settings"> 57 <CheckBoxPreference58 android: defaultValue = "true" 59 android: icon = "@ drawable/ic_launcher" 60 android: key = "checkbox_key_wangluo" 61 android: summary = "Network enabled" 62 android: title = "network"/> 63 </PreferenceScreen> 64 65 </PreferenceScreen>


Load SettingFragment from the master Acitivity:

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 @ Override11 protected void onCreate (Bundle savedInstanceState) {12 super. onCreate (savedInstanceState); 13 // create the Fragment manager 14 FragmentManager fm = this. getFragmentManager (); 15 FragmentTransaction ft = fm. beginTransaction (); 16 // replace the original Activity with Fragment. You can also add it to the original directory without overwriting it, for example, ft. add (R. id. rel, new17 // Fragment (). If you add more, it will consume resources. 18 ft. replace (android. R. id. content, new SettingFragment (); 19 // submit 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. P ReferenceManager; 11 import android. preference. SwitchPreference; 12 13 // android officially states that PreferenceFragment should be used instead of FragmentActivity after version 3.0 and called from the main Activity. 14 public class extends limit {15 16 private MyListener mMyListener = new MyListener (); 17 private EditTextPreference mEditTextPreference; 18 private ListPreference mListPreference; 19 private CheckBoxPreference mCheckBoxPreference; 20 private SwitchPreference limit; 21 private SwitchPreference mSwitchPreference2; 22 23 @ Override 24 public void onCreate (B Undle savedInstanceState) {25 super. onCreate (savedInstanceState); 26 // common method for reading values 27 SharedPreferences sharedpreferences = PreferenceManager. getdefasharsharedpreferences (getActivity (); 28 29 // Add setting option 30 addPreferencesFromResource (R. xml. preferences); 31 // find the control buttons 32 mEditTextPreference = (EditTextPreference) findPreference ("edit_text_key_1"); 33 34 mListPreference = (ListPreference) findPreferen Ce ("listpreference"); 35 36 mCheckBoxPreference = (CheckBoxPreference) findPreference ("checkbox_key_xuanzhuan"); 37 38 mSwitchPreference1 = (SwitchPreference) findPreference ("wifi "); 39 mSwitchPreference2 = (SwitchPreference) findPreference ("lanya"); 40 // set the initial status of the prompt box according to the actual situation 41 boolean s1 = sharedpreferences. getBoolean ("wifi", false); 42 if (! S1) 43 mSwitchPreference1.setSummary ("wifi disabled"); 44 else if (s1) 45 mSwitchPreference1.setSummary ("wifi enabled"); 46 47 boolean s2 = sharedpreferences. getBoolean ("lanya", false); 48 if (! S2) 49 mSwitchPreference2.setSummary ("Bluetooth disabled"); 50 else if (s2) 51 mSwitchPreference2.setSummary ("Bluetooth enabled"); 52 53 boolean s = sharedpreferences. getBoolean ("checkbox_key_xuanzhuan", false); 54 if (! S) 55 mCheckBoxPreference. setSummary ("turn off"); 56 else if (s) 57 mCheckBoxPreference. setSummary ("turn on"); 58 59 // listen to 60 sharedpreferences when the project data is added to change. unregisterOnSharedPreferenceChangeListener (mMyListener); 61} 62 // cancel the registration listening event when the program is paused, making the code more complete. 64 @ Override 65 public void onPause () {66 super. onPause (); 67 getPreferenceScreen (). getSharedPreferences (). unregisterOnSharedPreferenceChangeListener (mMyListener); 68} 69 70 // Register 71 @ Override 72 public void onResume () {73 super. onResume (); 74 getPreferenceScreen (). getSharedPreferences (). registerOnSharedPreferenceChangeListener (mMyListener); 75} 76 77 private class MyListener implements O Listen {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 ci Ty_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 ("turn off"); 98 else if (s) 99 mCheckBoxPreference. setSummary ("turn on"); 100 101 102 break; 103 case "wifi": boolean s1 = sharedPreferences. getBoolean ("wifi", false); 104 if (! S1) 105 mSwitchPreference1.setSummary ("wifi off"); 106 else if (s1) 107 mSwitchPreference1.setSummary ("wifi on"); 108 109 break; 110 case "lanya ": 111 112 boolean s2 = sharedPreferences. getBoolean ("lanya", false); 113 if (! S2) 114 mSwitchPreference2.setSummary ("Bluetooth disabled"); 115 else if (s2) 116 mSwitchPreference2.setSummary ("Bluetooth enabled"); 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> Chengdu </item> 6 <item> Beijing </item> 7 <item> Shanghai </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>

Run:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.