Android setting option development and custom Preference styles
A complete Android Application should provide options (or preference settings, etc.) so that users can set the APP's representation, such as whether to join the user experience plan, or automatic upgrade, scheduled reminder, Automatic startup, background running, etc. A set item is provided to greatly improve the user experience of the APP. To complete this function, you do not have to write Activity or Fragment from the beginning, because Android already provides an API to implement this function, in addition, the user settings are automatically saved to SharedPreference (one of the four storage methods of Android) in the form of key-value pairs. PreferenceActivity is used in systems earlier than 3.0. This class is discarded in Apis after api level 11 (Android 3.0) and PreferenceFragment is used instead. The usage and function calls of the two are similar, which can be measured by the target system version of the app. This article mainly describes two problems, with a low level, focusing on the summary and basic usage. 1. It is very easy to add setting options for apps on the Android platform. PreferenceFragment is used as an example to demonstrate how the times are moving forward. The parent class of PreferenceFragment is the Fragment class, And the Fragment object must be embedded in the Activity for display. Therefore, we can determine how to create an activity for the setting, and then embed the PreferenceFragment subclass object into it. The option setting is basically implemented because the data is automatically saved and updated. The idea is very simple. I suggest you stick to the main code and sort out the ideas to help you understand them. First, create a Preference configuration file for the setting options. The layout file is also in XML format and hierarchical. Note that it is stored in res/xml instead of res/layout. The system also provides some frequently-used setting options, such as PreferenceScreen, PreferenceCategory, CheckBoxPreference, EditTextPreferece, and ListPreference. If needed, you can easily implement custom Preference, the implementation method is described below. Create a Preference and name it settings. xml (more traditionally, preference. xml ). Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <PreferenceScreen xmlns: android = "http://schemas.android.com/apk/res/android" 3 xmlns: preference = "http://schemas.android.com/apk/res/com.test.mytest" 4 android: title = "Settings"> 5 6 <PreferenceScreen 7 android: title = "about"> 8 <Preference android: title = "feedback"> 9 </Preference> 10 11 <com. test. mytest. preferenceWithTip12 preference: tipstring = ">" 13 preference: titlestring = "Custom test"> 14 <intent15 android: action = "Android. intent. action. VIEW "16 android: data =" http://www.baidu.com "/> 17 </com. test. mytest. preferenceWithTip> 18 19 <Preference android: title = "FAQ"> 20 </Preference> 21 <Preference android: title = "check for updates"> 22 </Preference> 23 <Preference android: title = "Copyright Disclaimer"> 24 </Preference> 25 26 <SwitchPreference27 android: key = "setting_test" 28 android: title = "test"/> 29 </PreferenceScreen> 30 31 </PreferenceScreen> copy the code Then create an Acitivity for the setting options, because the PreferenceFragment sub-class is very simple to write here, by the way, implemented by the internal class. Copy code 1 public class SettingsActivity extends Activity {2 3 @ Override 4 public void onCreate (Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 setContentView (R. layout. settings); 7 setTitle ("option settings"); 8 getFragmentManager (). beginTransaction (). replace (R. id. settings_content, 9 new PrefsFragment ()). commit (); 10} 11 12 public static class PrefsFragment extends PreferenceFragment {13 14 @ Override15 public void onCreate (Bundle savedInstanceState) {16 super. onCreate (savedInstanceState); 17 addPreferencesFromResource (R. xml. settings); 18} 19 20} 21 22} after the code is copied, the user clicks the configured setting option. You should have guessed it here, enable the setting option, but only open an Intent. The basic process is here, but a setting that requires a large number of users is much more complicated than this. You may need to adjust the performance of the application according to the user's settings, onPreferenceTreeClick (PreferenceScreen preferenceScreen, Preference prefence) may be implemented ). As mentioned above, just getting started with Preference only summarizes the basic usage here. 2. Use the custom Preference class in the setting options to directly inherit from the Object class. In the preceding settings. in xml, several Preference definitions are defined. Preference only provides simple text display, while its subclass CheckBoxPreference, SwitchPreference, and EditTextPreference provide more complex UI display, it can also save user setting data. In general, these sub-classes Preference are more important for applications. If you use these subclass objects, they are actually very simple. They can be used in the Preference-defined xml file similar to the usage of the UI control in Layout (settings. in xml), you can use the eclipse code prompt function. These usage basics are not the focus of this article. The following describes how to define your Preference. Figure 1 custom Preference display Figure 1 shows the differences between Preference and custom Preference styles. You may notice that the second "Custom test" has only one ">" symbol difference with other Preference, in fact, this includes the complete step for customizing a Preference. By the way, the methods and routines of custom Preference are almost the same as those of custom controls. Summarize the basic steps. 1) define the property value attr. xml to copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 <declare-styleable name = "PreferenceWithTip"> 4 <attr name = "tipstring" format = "string"> </attr> 5 <attr name = "titlestring ""format =" string "> </attr> 6 </declare-styleable> 7 </resources> copy Code 2) design preferencewithtip for custom Preference layout. xml 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "horizontal" 6 android: paddingLeft = "8dp" 7 android: paddingRight = "15dp" 8 android: paddingTop = "20dp" 9 android: paddingBottom = "20dp"> 10 <TextView 11 android: id = "@ + id/prefs_title" 12 android: layout_width = "0dp" 13 android: layout_heig Ht = "wrap_content" 14 android: layout_gravity = "left" 15 android: gravity = "left | center_vertical" 16 android: textSize = "18sp" 17 android: layout_weight = "1"/> 18 <TextView 19 android: id = "@ + id/prefs_tip" 20 android: layout_width = "0dp" 21 android: layout_height = "wrap_content" 22 android: layout_gravity = "right" 23 android: gravity = "right | center_vertical" 24 android: textSize = "18sp" 25 android: layout_weight = "1"/> 26 27 </Line ArLayout> 3) inherit Preference and implement your Preference class PreferenceWithTip 1 public class PreferenceWithTip extends Preference {2 private static final String TAG = "PreferenceWithTip"; 3 String pTitle = null; 4 String tipstring = null; 5 6 @ SuppressLint ("Recycle") 7 public PreferenceWithTip (Context context, AttributeSet attrs, int defStyle) {8 super (context, attrs, defStyle ); 9 // obtain the custom parameter 10 Log. I (TAG, "Prefere NceWithTip invoked "); 11 TypedArray ta = context. obtainStyledAttributes (attrs, R. styleable. preferenceWithTip); 12 tipstring = ta. getString (R. styleable. preferenceWithTip_tipstring); 13 pTitle = ta. getString (R. styleable. preferenceWithTip_titlestring); 14 ta. recycle (); 15} 16 17 public PreferenceWithTip (Context context, AttributeSet attrs) {18 this (context, attrs, 0); 19} 20 21 @ Override22 protected void o NBindView (View view) {23 super. onBindView (view); 24 TextView pTitleView = (TextView) view. findViewById (R. id. prefs_title); 25 pTitleView. setText (pTitle); 26 TextView pTipView = (TextView) view. findViewById (R. id. prefs_tip); 27 pTipView. setText (tipstring); 28} 29 30 @ Override31 protected View onCreateView (ViewGroup parent) {32 return LayoutInflater. from (getContext ()). inflate (R. layout. preferencewithtip, 33 pa Rent, false); 34} 35 36 // if you want to update or save data, you need to write 37 38} 4) Call. The calling code has been posted at the beginning of the article. The main code is as follows. preference is the custom package name. Copy code 1 <com. ict. customview. preferenceWithTip2 preference: tipstring = ">" 3 preference: titlestring = "Custom test"> 4 <intent5 android: action = "android. intent. action. VIEW "6 android: data =" http://www.baidu.com "/> 7 </com. ict. customview. preferenceWithTip> copy the code to summarize the usage of Preference. Custom Preference is also convenient. However, it is not easy to design a beautiful and humanized Preference. However, these are all ways to improve the user experience and deserve further exploration.