Explain the use examples of Android preference framework in detail

The term preference will not be unfamiliar to friends familiar with Android. It is often used to set the running parameters of software.

Android provides a robust and flexible framework to handle preferences. It provides a simple API to hide the reading and persistence of preferences, and provides an elegant preference interface.

First, let's take a look at the following software preferences interface:

This software uses several types of preferences, each of which has its own unique usage. Let's learn about several common preferences:

When we use the preference framework, every time a user changes the value of an item, the system will immediately display it in / data / data / [package_name] / shared_ Generate a [package_name] under prefs_ Preferences.xml, which records the latest configuration information.

So how do I use the preferred framework? We need the following steps:

1. Create a preference XML configuration file and put it under the / RES / XML directory of the project;

2. Create a new activity, inherit android.preference.preferenceactivity, and then load our preference configuration file in oncreate method.

Next, I'll show you the configuration and use of the preference framework:

We will create a prefs project with the following structure:

The functions we want to achieve are very similar to those of the above software. Let me explain the overall process of the project:

1. Main interface. Displays the user's nickname with three parameters: nickname text, font size and background color. The default value is used when entering for the first time.

2. Press the settings item in the menu key to jump to the Preferences page for parameter selection.

3. Press the return key to return to the main interface, and the set parameters will take effect.

First, let's take a look at the configuration file of the main interface. It's very simple. It's a textview:

Then, we need to set the appearance and background of textview in the main interface according to the configuration parameters. The code of mainactivity.java is as follows:

As you can see, after entering the main interface, you will_ Preferences gets the configuration information of preferences. If it is the first time to enter, the default value will be used. The following is the screen when we enter the main interface for the first time:

It can be seen that when we first entered the interface, we nicknamed it "robot", the background of the word was white, and the size of the word was 18.

Then press settings to configure the preferences. Let's take a look at the configuration of settings.xml:

The outermost layer is the preferencescreen tag, which represents a set of preferences; Then, we notice the item preferencecategory, which represents a category and can contain multiple preferences; Finally, there are our preferences for configuring parameters. It should be understood that preferencescreen can also be nested, that is, the above preferencecategory can be replaced by preferencescreen.

In addition, we also need to understand several common tag attributes in the file:

We can also see that in listpreference, entries are from textsize_ Entry, and entryvalues comes from textsize_ entry_ Value, both of which are in text in the / RES / values directory_ Size.xml configuration:

After the configuration is completed, we have the last step left. Create an activity, inherit the preferenceactivity, load the preference resource file, and handle the corresponding option events.

The prefsactivity.java code is as follows:

Finally, don't forget to add this activity configuration information to androidmanifest.xml:

Of course, we can also configure the < intent Filter > < / intent Filter > attribute.

After the above steps, our preference configuration is completed. The first interface is as follows:

The contents are as follows:

At this time, we press the back button to return to the main interface and find that the settings just now have taken effect:

If we press clear history on the preferences screen, the following interface will appear:

Original link: http://blog.csdn.net/liuhe688/article/details/6448423

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>