Features and usage of the search box (Searchview) in Android

Source: Internet
Author: User

1, Searchview is the search box component, it can let the user enter text in the text box, through the listener to obtain the user's input, when the user clicks the search, the listener performs the actual search.


2. The common methods of Searchview components are as follows:

①seticonifiedbydefault (Boolean iconified) ===> sets whether the search box is automatically reduced to an icon by default.
②setonquerytextlistener (Searchview,onquerytextlistener Listener) ===> Set listener for search box
③setsubmitbuttonenabled (Boolean enabled) ===> setting whether the search button is displayed
④setqueryhint (charsequence hint) ===> set the default display prompt text in the search box


3, add a matching listview for Searchview, you can add auto-complete function for it, that is, ListView is used to display auto-completion list for Searchview.


4, the specific implementation code as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayoutxmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " ><!--Top One searchview--><searchviewandroid:id= "@+id/sv" android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content"/><!--define auto-filled listview--><listviewandroid:id= "@+id/lv" for Searchview Android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "1"/></LinearLayout >

Package Org.crazyit.ui;import Android.os.bundle;import Android.text.textutils;import android.widget.ArrayAdapter; Import Android.widget.listview;import Android.widget.searchview;import Android.widget.toast;import Android.app.activity;public class Searchviewtest extends Activity implementssearchview.onquerytextlistener {private Searchview sv;private ListView lv;//Auto-complete list private final string[] mstrings = {"AAAAA", "bbbbbb", "CCCCCC", "DDDDDDD"};@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); LV = (ListView) Findviewbyid (r.id.lv); Lv.setadapter (New Arrayadapter<string> (this,android. R.layout.simple_list_item_1, mstrings)); lv.settextfilterenabled (true);//Set LV can be over-charged SV = (Searchview) Findviewbyid ( R.ID.SV);//sets whether the Searchview default automatically zooms out to the icon Sv.seticonifiedbydefault (false);// Set the event listener Sv.setonquerytextlistener (this) for the Searchview component, or//set the Searchview to display the search button sv.setsubmitbuttonenabled (true);// Sets the prompt text that is displayed by default within the Searchview Sv.setqueryhiNT ("Find");} The method is fired when the user enters a character @overridepublic boolean onquerytextchange (String newtext) {toast.maketext (Searchviewtest.this, " TextChange---> "+ newtext, 1). Show (); if (Textutils.isempty (NewText)) {//Clears the filter lv.cleartextfilter for the ListView ();} else { Filter the list items of a ListView using user input Lv.setfiltertext (newtext);} return true;} The method is fired when the search button is clicked @overridepublic Boolean onquerytextsubmit (String query) {//actual application should execute the actual query within the method// Only Toast is used here to display user-entered query content Toast.maketext (this, "Your choice is:" + query, Toast.length_short). Show (); return false;}



Features and usage of the search box (Searchview) in Android

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.