Use of preferenceactivity and implementation of the example interface (2)

Source: Internet
Author: User
In the previous blog, we introduced in detail the preparations for layout in the development example of preferenceactivity. Although the preparation is very simple, I have tried to write a blog post on the Internet, I hope you will be considerate.

There is no doubt that Android has been very popular over the past few years. In a short period of time, thousands of developers have accumulated crazy about it, and I am also one of the thousands, the powerful functions of the SDK have deeply attracted me. I have developed Symbian programs slightly before, which is very painful. However, after the war on Android, the situation is much better than before, thanks to the open-source and rapid development of Android, as well as excellent explanations and sample code provided by many Android enthusiasts. Well, let's just move on to the topic and start the code phase of PreferenceActivity for Android development. First, in the previous blog, we have created a Preference XML file preference. xml, which records our layout. How can we add the layout file to the code? In the traditional Layout layout, the system will generate the code setContentView (R. Layout. main) by default. Is this the case when Preference is used? I believe that Preference can be used as a layout file, but when I load it into the simulator (setContentView (R. xml. preference), the following error is returned: Caused by: android. view. inflateException: Binary XML file line #2: Error inflating class PreferenceScreen Caused by: java. lang. classNotFoundException: android. view. preferenceScreen in loader dalvik. system. the code line with an error in PathClassLoader @ 44c17b30 is "setContentView (R. xml. preference) ", I haven't figured it out yet. I have to wait for the next blog post to analyze it in detail. In fact, in PreferenceActivity development, the current class must inherit PreferenceActivity rather than Activity. When adding XML code, addPreferenceFromResource (R. xml. preference); run the project we created. The interface is as follows. Are you familiar with this? Because many configuration interfaces adopt this style, which is not only suitable for many applications, but also suitable for many games. It is a program that Android Developers must master.

So here, the basic interface layout has been fully tested, so the next thing, of course, is the control response and data operations. In preferenceactivity, the acquisition of a control is different from that of a normal layout. The former is findpreference (string s), and the latter is findviewbyid (INT layoutid). The parameter passing is also the same, the former is the key of the control, that is, in preference. the key defined by the variable in XML identifies the control, and then various operations can be performed on the control. Common Operations on the control are as follows: public Boolean onpreferencechange (preference, object newvalue) {} is the user's response to changes to the control. Public Boolean onpreferenceclick (preference) {} is the response to the user clicking preference. It is generally seen in the response to the preference of the text, for example, the usage instructions of the software. Prompts you about software operations and developer copyrights. So let's not talk about it. Continue to the development process. As mentioned above, if you click the Start Software button, how can the system capture it? First, the system needs to determine which preference control is pressed, and the onpreferencechange function is for the Global. In this case, it identifies which control is pressed as the key of the control, the description is as follows: if (preference. getkey. equals ("your preference key") {} else if () {} to know which control is needed to generate a response; however, it is useless to know which control generates more responses. We need to know the status (for example, whether the checkbox is enabled or disabled ?) In preferenceactivity, after a user clicks the response preference, the value of this preference (for example, the value of checkboxpreference is true or false, respectively, to identify whether to enable the preference, the value of edittextpreference is the value entered in the input box) is in/data/you_package_name/shared_prefs/you_package_name_you_xml_name.xml, And the identifier of each option is the key corresponding to the control, therefore, for preferenceactivity, the key of a control (that is, ID is very important, if you want to use the value corresponding to this preference ), android provides developers with this method to directly obtain the value of a preference. The specific method is as follows (taking checkboxpreference as an example): sharedpreferences prefs = preferencemanager. getdefasharsharedpreferences (this );

Boolean value = prefs. getstring ("checkboxpreference", "true ");

If you have experience with sharedpreferences, you should know that the function is to obtain the value stored in the checkboxpreference field. The default value is "true" and the return value is boolean, this may be why this activity is called preferenceactivity. For ease of development, I often encapsulate methods for data operations into a complete class. Operations on data can only be completed by calling related methods by using the objects of this class, to complete some of the remaining functions of this program, the data operation classes written by the author are as follows (for reference only ): Public  ClassSunshine_preference_dataoption
{// Declaration and definition of related variables sharedpreferences sp = Null; // Constructor PublicSunshine_preference_dataoption (Context
Context) {sp = preferencemanager. Getdefasharsharedpreferences(Context); // implement initialization} // obtain the value in checkboxpreference. The returned value is boolean. Public  BooleanGetcheckboxpreferencevalue (){ BooleanValue = sp. getboolean ("Your
Checkboxpreference key ", True); // Replace the variable with your own key. By default, true is returned; ReturnValue ;}} here I only use the code to obtain the checkboxpreference value for your reference. Other preference values are the same. As mentioned above, a single preference, that is, a preference that generates software help documentation and developer copyright information, does not have relevant parameter storage and does not naturally have parameter changes. At this time, how does it generate a dialog? Here, it uses the public Boolean onpreferenceclick (preference) {} function to generate a dialog when detecting a preference (Your copyright information and preference for help, deploy all the information you want. In fact, during the process of data changes, a function has been listening for changes in the value of a preference and can be updated in a timely manner, this is because I want to display the updated data on listpreference in the form of summary after updating the listpreference data. Do you have a better method? I tried thread, handler, and other methods, but when I looked at the API, I accidentally saw this method. Now that we have this method, naturally, it is easy to implement the above functions. The specific method is to obtain the operation preference and then change it. The specific method is as follows: Public  VoidOnsharedpreferencechanged (sharedpreferences
SharedPreferences, String key ){ If(Key. equals ("listpreference") {// The data updated in your listpreference. For details, see listpreference_num.setSummary ("your
Listpreference num ") ;}} to implement the functions described above.
The author's conclusion: It took me one night to write about preferenceactivity, which was used in almost all kinds of operations to write this blog post, this is mainly to provide more developers with a more convenient place to query information, so as not to spend more time and energy. The summary of preferenceactivity is incomplete, but I believe that, with some simple introductions above, you should be familiar with preferenceactivity. Some basic operations have been described in detail in the blog. Of course, if you have any questions, can consult me: zhangganhan@126.com.
We look forward to hearing from you ~

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.