In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to understand the Android preference framework ListPreference. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Explore the preferences framework
Before delving into Android's * * items framework, first imagine a scenario where you need to use * * items, and then analyze how to implement this scenario. Suppose you are writing an application that provides a tool to search for aircraft flights. Also, assume that the default setting for the application is to display flights based on the order of ticket prices from low to high, but the user can preferably set the item to always fly based on the minimum number of stops or a specific route. How to achieve this scenario?
ListPreference
Obviously, the user must be provided with a UI to view the list of sorting options. The list will contain radio buttons for each option, and the default (or current) option should be pre-selected. Very little work has been done to solve this problem using the Android*** items framework. First, create a * item XML file to describe * items, and then use a pre-built activity class that knows how to display and persist * items. Here is our * item XML file flightoptions.xml.
Xml code
It says above that we also need an Activity class FlightPreferenceActivity, so let's make our Activity class. The Activity class inherits the PreferenceActivity code as follows:
Java code
Package xiaohang.zhimeng; import android.os.Bundle; import android.preference.PreferenceActivity; public class FlightPreferenceActivity extends PreferenceActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); addPreferencesFromResource (R.xml.flightoptions);}}
The code listing above contains a XML snippet that represents the settings of the * * item of the flight options example. This contains an active class, our FlightPreferenceActivity, which is mainly used to load our XML files. First take a look at XML. Android provides an end-to-end framework for getting items. This means that the framework supports defining * items, displaying settings to the user, and persisting the user's selection to the database store. Define the * entry in the XML file in the / res/xml/ directory. To display * * items to the user, write an activity class to extend the predefined Android class android.preference.PreferenceActivity, and then use the addPreferencesFromResource () method to add resources to the active resource collection. The framework is responsible for the remaining operations (reality and persistence).
In this flight scenario, create the file flightoptions.xml under the / res/xml/ directory. Then create the active class FlightPreferenceActivity, which extends the android.preference.PreferenceActivity class. Next, call addPreferencesFromResource () to pass in R.xml.flightoptions. Note that the * item resource XML points to multiple string resources. To ensure that you compile correctly, you need to add multiple string resources to the project (which we'll cover later). Now let's take a look at what UI is generated from the above code listing.
There are two views above. The view on the left is called the * items screen, and the UI on the right is a list of items. When the user selects Flight Options, the Choose Flight Options view appears as a modal dialog box with radio buttons for each option. After the user selects an option, it immediately closes the view. When the user returns to the options screen, the view reflects the previously saved selections.
As mentioned earlier, the * * item XML file and the related activity classes, from above we can see that we defined a PreferenceScreen in the XML file, and then created the ListPreference as a subscreen. For PreferenceScreen, three properties are set: key, title, and summary. Key is a string that can be used to represent items programmatically (similar to using android:id); title is the title of the screen (My Preferences); and summary is a description of the purpose of the screen. For list * * items, set key, title, and summary, as well as entries, entryValues, dialogTitle, and defaultValue features. The following table summarizes these features.
Property description the name of the android:key option or key (such as selected_flight_sort_option) the title of the android:title option the short summary of the android:summary option android:entries sets the option to the text of the list item the android:entryValues defines the value of each list item. Note: each list item has some text and a value. The text is defined by entries and the value is defined by entryValues. Android:dialogTitle
The title of the dialog box, used when the view is displayed as a modal dialog box
Default values for options in the android:defaultValue item list
In order for our example to work properly, we also need to add the following files.
Xml code
Total Cost # of Stops Airline 0 1 2
You can see at a glance that this file is the displayed text that defines our options, and the corresponding value. The name of this XML file is arrays.xml. This file should be placed in / res/values/arrays.xml.
Of course we can't lose our strings.xml.
Xml code
Hello World, FlightPreferenceActivity! Preference_Demo My Preferences Set Flight Option Preferences 1 Choose Flight Options Set Search Options Flight Options selected_flight_sort_option Settings Quit
It was mentioned earlier that we have a * item view, that is, the FlightPreferenceActivity, which is here.
In this example, we jump to our item view through a menu. That is, we have a MainActivity, this MainActivity has a menu called Settings, and when we click on this menu, we jump to our * item view, and then we modify the contents of the item, and when we go back to MainActivity again, we see our modified text and value, and we display it through a TextView.
Let's take a look at the effect.
The following XML file is used to define our menu. The directory of this file is in / res/menu/mainmenu.xml
Xml code
It's easier, too.
Next is our layout file main.xml.
Xml code
There is only one TextView that is mainly used to display the text and values after we modified the * items.
With main.xml, you can't do without MainActivity, so let's make our MainActivity class
Java code
Package xiaohang.zhimeng; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends Activity {private TextView tv = null; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.main); tv = (TextView) findViewById (R.id.text1); setOptionText ();} / create a menu that we defined in the XML file and loaded here to OK @ Override public boolean onCreateOptionsMenu (Menu menu) {MenuInflater inflater = getMenuInflater () / / load our menu file inflater.inflate (R.menu.mainmenu, menu); return true } / / menu option event @ Override public boolean onOptionsItemSelected (MenuItem item) {if (item.getItemId () = = R.id.menu_prefs) {/ / when we click on the Settings menu, it jumps to our * item view That is, our FlightPreferenceActivity Intent intent = new Intent () .setClass (this, xiaohang.zhimeng.FlightPreferenceActivity.class) / / since the last Activity is the data returned by our * * item view, we use the startActivityForResult () method to start our * item view / / Parameter 1: where are we going to jump to / / Parameter 2: echo code this.startActivityForResult (intent, 0) } else if (item.getItemId () = = R.id.menu_quit) {/ / when we click the Quit menu to exit the application finish ();} return true } / / this method is used to receive the data returned by our last Activity, that is, our * * item view, because we may modify the data @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data); setOptionText () } / / this method is used to set the value of TextView on our MainActivity (that is, the value of our * item) private void setOptionText () {/ * this method is more interesting * parameters: used to specify the name of the file in which we store our * item value * format is the package name _ preferences You can see that my package name is xiaohang.zhimeng * here if you don't write according to this format such as if you don't write your current package name as something else. Will also generate the current package name _ preferences this file will write or not write it right there * second parameter: open mode * * / SharedPreferences prefs = getSharedPreferences ("xiaohang.zhimeng_preferences", 0) / / look at the document in this method, otherwise I will write String option = prefs.getString (this.getResources (). GetString (R.string.selected_flight_sort_option), this.getResources (). GetString (R.string.flight_sort_option_default_value)) / / get the text of all our options in the * item String [] optionText = this.getResources (). GetStringArray (R.array.flight_sort_options); / / set the value tv.setText ("option value is" + option + "(" + optionText[ Integer.parseInt (option)] + ")) to be displayed by our TextView;}}
If you are new to this place, such as what SharedPreferences is, you can refer to these two articles.
Http://byandby.iteye.com/blog/837601
Http://byandby.iteye.com/blog/833292
Below is our AndroidManifest.xml file, which is nothing special.
Xml code
OK, when we have finished running all the applications above, we will first see a simple text message showing "option value is 1 (# of Stops)". Click the Menu button, and then click Settings, which will open our * * item view FlightPreferenceActivity, then we will change the value of the * item, and then click the back button to see our modified value.
You may ask, where did Android store our modified data? As mentioned earlier, Android framework will also be responsible for persisting * * items. For example, when the user selects a sort option, Android chooses to store it in a XML file in the application / data directory, as shown in the following figure.
The actual file path is / data/data/ [packet _ NAME] / shared_prefs/ [packet _ NAME] _ preferences.xml. We need to see what is stored in this file. Export this file and you can see it. Oh no, it's too much trouble. Let's go to shell and read it with cat. See the picture below.
It can be seen at a glance that it is accessed in the way of key-value pairs.
The above is the editor for you to share how to understand the Android preference framework ListPreference, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.