Android authoritative Programming Guide-Notes (25th Chapter Search)

Source: Internet
Author: User

1. Searchview

Searchview is an operation view, the so-called Operation View, which is a view that can be built into the toolbar. Searchview allows the entire search interface to be fully built into the app's toolbar.

Configuration of the 1.1 Searchview

<?XML version= "1.0" encoding= "Utf-8"?><Menuxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto">    <ItemAndroid:id= "@+id/menu_item_search"Android:title= "@string/search"App:actionviewclass= "Android.support.v7.widget.SearchView"app:showasaction= "Ifroom" /></Menu>

APP:ACTIONVIEWCLASS Specifies the Android.support.v7.widget.SearchView value, telling the toolbar to display Searchview.

The menu XML file is then instantiated through the Oncreateoptionsmenu () method, so that the toolbar can display the options defined in the menu XML.

Use of 1.2 Searchview

The interface on which to instantiate and then set the callback.

MenuItem Searchitem = Menu.finditem (R.id.menu_item_search);//Remove the MenuItem and save it in the Searchitem variable. Msearchview = (Searchview) Searchitem.getactionview ();//Remove the Searchview object. //Setting up ListenersMsearchview.setonquerytextlistener (NewSearchview.onquerytextlistener () {@Override Public BooleanOnquerytextsubmit (String s) {//when you submit a search query. This method executes. LOG.D (TAG, "Onquerytextsubmit:" +s); return true; } @Override Public BooleanOnquerytextchange (String s) {//This method executes when the text of the search box changes. LOG.D (TAG, "Onquerytextchange:" +s); return false; }         });

2. Using shared preferences for lightweight data storage

Shared preferences is essentially a file in the file system and can be read and written using the Sharedpreferences class. The Sharedpreferences instance is more like a key-value pair warehouse (similar to Bundle), but it can persist data through persistent storage. The key in a key-value pair is a string, and the value is an atomic data type. Further viewing the shared preferences file, they are actually a simple XML file, but the Sharedpreferences class has masked the implementation details of the read-write file. The shared preferences file is saved in the app sandbox, so sensitive information like a password should not be saved with it.

To obtain a custom sharedpreferences instance, you can use the Context.getsharedpreferences (String,int) method. However, in actual development, we do not care about what the sharedpreferences instance is, as long as it can be shared throughout the application. In this case, it is best to use the Preferencemanager.getdefaultsharedpreferences (context) method, which returns an instance with a private permission and a default name (available only within the current app).

Shared Preference Example:

 Public classquerypreferences {Private Static FinalString pref_search_query = "SearchQuery";  Public StaticString Getstoredquery (context context) {returnPreferencemanager.getdefaultsharedpreferences (context). getString (Pref_search_query,NULL); }     Public Static voidSetstoredquery (context context, String query) {preferencemanager.getdefaultsharedpreferences (context). Edit (). P Utstring (Pref_search_query, QUERY). Apply ();
}}

Android authoritative Programming Guide-Notes (25th Chapter Search)

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.