Application preferences of Xamarin. Android Learning

Source: Internet
Author: User

Application preferences of Xamarin. Android Learning
I. Preface any App will have a configuration interface. If developers use common controls and bind listening events to save the settings, this process will be very boring and time-consuming. We can see that there are so many options in the setting interface of the Android system, are they all developed in this way? In fact, Android has already provided us with a technology specifically designed for this feature, called application preferences. Today we will learn how to use them to develop configuration interfaces and features. 2. The first thing we need to understand in preparation is that we still need controls in setting the interface, but the controls we use are not common controls, next we will briefly introduce the controls we need to know. CheckBoxPreference: used to check items. Saved as bool type in SharedPreference. EditTextPreference: The project used to implement character input. It is saved as a string type in SharedPreference. ListPreference: used to provide a column of data for selection. It is saved as a string type in SharedPreference. PreferenceActivity: preference activity. PreferenceCategory: used to classify preferences. PreferenceScreen: used to group preferences on a new screen. In addition to the above, there are other examples that do not make sense here. Iii. Body 1. to display a simple preference, we first need to open MainActivity and change the inherited base class to PreferenceActivity, and then change SetContentView to AddPreferencesFromResource. The Code is as follows: 1 protected override void OnCreate (Bundle bundle) 2 {3 base. onCreate (bundle); 4 AddPreferencesFromResource (Resource. layout. main); 5} the reader can also try to display it directly without modification. Of course, an error will certainly be reported. After the code is modified, OPEN Main. axml and delete all the xml in it, and rewrite it as follows: 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <PreferenceScreen xmlns: android = "http://schemas.android.com/apk/res/android"> 3 <PreferenceCategory 4 android: title = "configuration Classification 1"> 5 <CheckBoxPreference 6 android: key = "PREF_CHECK_BOX" 7 android: title = "Check Box Preference" 8 android: defaultValue = "true"/> 9 </PreferenceCategory> 10 <PreferenceCategory11 android: title = "configuration category 2"> 12 <EditTextPreference13 android: key = "PREF_EDIT_BOX" 14 android: title = "Edit Tex T Preference "15 android: dialogMessage =" please input "16 android: defaultValue =" test "/> 17 </PreferenceCategory> 18 <PreferenceCategory19 android: title = "configuration category 3"> 20 <ListPreference21 android: title = "List Preference" 22 android: key = "listChoice" 23 android: entries = "@ array/ListText" 24 android: entryValues = "@ array/ListValue" 25 android: summary = "choice one item"/> 26 </PreferenceCategory> 27 <PreferenceCategory28 Android: title = "configuration category 4"> 29 <PreferenceScreen30 android: title = "sub-configuration"> 31 <CheckBoxPreference32 android: key = "PREF_CHECK_BOX_1" 33 android: title = "Check box" 34 android: defaultValue = "true"/> 35 </PreferenceScreen> 36 <PreferenceScreen37 android: title = "open new intent"> 38 <intent39 android: action = "android. settings. DISPLAY_SETTINGS "/> 40 </PreferenceScreen> 41 </PreferenceCategory> 42 </PreferenceScreen> copying a part of the code is simple and I will not do more Introduction. Mainly refer to the xml code: 1 <PreferenceScreen2 android: title = "open new intent"> 3 <intent4 android: action = "android. settings. DISPLAY_SETTINGS "/> 5 </PreferenceScreen> the entries and entryValue resources in ListPreference are as follows (Strings. in xml): Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 <string name = "Hello"> Hello World, Click Me! </String> 4 <string name = "ApplicationName"> PreferenceScreen </string> 5 <string-array name = "ListText"> 6 <item> First </item> 7 <item> Second </item> 8 <item> Third </item> 9 </string-array> 10 <string-array name = "ListValue"> 11 <item> VFirst </item> 12 <item> VSecond </item> 13 <item> VThird </item> 14 </string-array> 15 </resources> copy the code and we know that PreferenceScreen will open a new interface to display its internal preferences, however, an internal intent indicates opening the activity of the specified action, just like star. Like tActivity, this is a bit helpful for us to set some of the settings we need, but this setting already exists in the system, we can link it in this way. Below we run the project, we can see the following image: I believe some people will feel this is something, the display effect is the same as that of ordinary controls. Because it uses common controls, and of course it won't be used in white, it has already helped you do a big push, each option on this interface has been automatically saved in SharedPreference, without binding events one by one, and then saving. To read these configurations, you only need to Get the ISharedPreference type objects through getdefasharsharedpreferences of PreferenceManager and then read them through the Get <type> method. Their key is naturally the android of the control: key Attribute Value. This saves a lot of time. 2. Although we can read the configuration value for the listener preference change, not all settings in the actual application will be enabled the next time. Some settings may need to modify the current function after modification. For example, if you set to disable a function, the app needs to immediately disable the function while selecting the function. At this time, we need to listen to the corresponding events, and we only need to implement the ISharedPreferencesOnSharedPreferenceChangeListener interface. The following code is the interface: Copy code 1 public interface ISharedPreferencesOnSharedPreferenceChangeListener, IDisposable2 {3 void OnSharedPreferenceChanged (ISharedPreferences sharedPreferences, string key); 4} copy the code and we can see that you only need to implement one method, the second parameter of this method is the android: key value of the option to be modified. With the key, we can get the modified value in the first parameter, you need to select the corresponding type of Get method as needed. To save time, we still use the project in the previous section to implement this interface in MainActivity. The Code is as follows: Copy code 1 public void OnSharedPreferenceChanged (ISharedPreferences sharedPreferences, string key) 2 {3 string value = ""; 4 switch (key) 5 {6 case "PREF_CHECK_BOX ": 7 {8 if (sharedPreferences. getBoolean (key, false) 9 value = "true"; 10 else11 value = "false"; 12} 13 break; 14 case "PREF_EDIT_BOX": 15 {16 value = sharedPreferences. getString (key, ""); 17} 18 break; 19 case "listChoice": 20 {21 val Ue = sharedPreferences. getString (key, ""); 22} 23 break; 24} 25 Toast. the value of MakeText (this, key + "is changed to" + value, ToastLength. short ). show (); 26} copy the code. The author only obtains the corresponding value based on the situation and displays it through Toast. It is not enough to complete the above. We also need to register it, therefore, we need to use RegisterOnSharedPreferenceChangeListener and UnregisterOnSharedPreferenceChangeListener methods. The following code is specific: Copy code 1 protected override void OnResume () 2 {3 base. onResume (); 4 PreferenceManager. getdefasharsharedpre Ferences (this ). registerOnSharedPreferenceChangeListener (this); 5} 6 7 protected override void OnPause () 8 {9 base. onPause (); 10 PreferenceManager. getdefasharsharedpreferences (this ). unregisterOnSharedPreferenceChangeListener (this); 11} copy the code here to remind readers that I used the above method to view the book c # Android Application development practices, however, the listening event will not be executed, so I used the above method for reference to Android books. Run the program again. After modifying a project, the following results will appear: 3. in the app preferences under the new platform, we will learn how to implement the app preferences after Android 3.0 to develop a settings interface that is compatible with tablets and mobile phones. In this section, I suggest you create a new project, and the Android SDK version must be 3.0 or later. First, we need to design the xml code for the new application preferences. The author's code is as follows: Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <preference-headers xmlns: android = "http://schemas.android.com/apk/res/android"> 3

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.