Development and use of SearchView, core features of Android Development (source code sharing)

Source: Internet
Author: User

On Android, search is a core user feature. Users can search for any available data, whether the content is stored on the device itself or needs to be accessed through the network. Android provides a search framework for users to create a consistent search experience, which can help you implement search applications. The Search framework provides two Search modes: A Search Dialog box (Search Dialog) is located at the top of the Search screen or a SearchView, which can be embedded into your layout. In these two cases, the Android system will help you search for the task that implements the delivery and execution of the search to a specific Activity. Shows the effect.

When you need to execute a Search dialog box or search for widgets, the system creates an Intent and stores the user Query, and then the system starts the Activity you declared in advance to execute the data Query ,, use it for data search. The following three steps are required.

(1) create a Search. XML file in the res/xml file.

Configure search dialog or widget settings in an XML file. It includes setting features, such as voice search, search suggestion, and prompt text search box.

 
 

(2) declare a SearchActivity

This Activiy receives the data of the Search user and displays the search result. Note that in order for the called Activity to find this SearchActivity, you must first declare Android search Activitydao to your list file. As shown below.

            
                     
         
         ...

SearchActivity must complete the following three steps. 1 receive query 2 search for your data 3 display results.

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.search);    handleIntent(getIntent());}@Overrideprotected void onNewIntent(Intent intent) {    setIntent(intent);    handleIntent(intent);}private void handleIntent(Intent intent) {    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {      String query = intent.getStringExtra(SearchManager.QUERY);      doMySearch(query);    }}

I saw a blog post saying that onNewIntent () does not know how long it will be released. I have studied the official document above. If we set android: launchMode to "singleTop", SerachActivity will call the onNewIntent () method to receive ACTION_SEARCH intent.

(3) create a search interface

The search interface contains two types: SearchDialog and SearchWidget. SerachDialog: by default, the Search dialog box is hidden. We call onSearchRequested () (when the user presses the search button) to display it at the top of the screen. SearchWidget: you can place the search box anywhere in the layout, but it usually needs to be combined with actionbar.

The Activity on the search interface must also be declared in the list file.

          
         
     

Public class OtherActivity extends Activity {private Button mStartSearch; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. search_invoke); // The mStartSearch = (Button) findViewById (R. id. btn_start_search); // start mStartSearch in the search box. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {onSearchRequeste D () ;}}) ;}// rewrite the onSearchRequested method @ Override public boolean onSearchRequested () {// in addition to the input query value, you can also bind some data Bundle appSearchData = new Bundle (); appSearchData. putString ("demo_key", "text"); startSearch (null, false, appSearchData, false); // true must be returned. Otherwise, the bound data is voided. return true ;}}


Related Article

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.