The simplest way to use searchview

Source: Internet
Author: User

As the name suggests, searchview is a search view, which is similar to the automatically matched input box described earlier. However, he has his own listener and can get the user input result in real time. For the searchview control, I strongly recommend you place it on the actionbar to highlight the search function. However, the actionbar knowledge may be involved here, so here we will show you what this control is.

Layout File

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <requestFocus />    <SearchView        android:id="@+id/searchView_id"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <ListView        android:id="@+id/listView_id"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" >    </ListView>    </LinearLayout>

Mainactivity. Java

Package COM. kale. searchview; import android. app. activity; import android. OS. bundle; import android. widget. searchview; import android. widget. searchview. onquerytextlistener; import android. text. textutils; import android. widget. arrayadapter; import android. widget. listview; import android. widget. toast; public class mainactivity extends activity implements onquerytextlistener {private searchview sview; private listview lview; // automatically completed list private final string [] mstrings = {"aaaa", "BBBB ", "CCCC"}; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); viewinit (); lview. setadapter (New arrayadapter <string> (this, android. r. layout. simple_list_item_1, mstrings); lview. settextfilterenabled (true); // sets whether the searchview is automatically reduced to sview by default. seticonified (false); // set the event listener sview for the searchview component. setonquerytextlistener (this); // sets the search button sview for this component. setsubmitbuttonenabled (true); // sets the prompt text sview displayed by default for this component. setqueryhint ("");} // method triggered when the user inputs a character @ override public Boolean onquerytextchange (string newtext) {// method stub automatically generated by todo if (textutils. isempty (newtext) {// clear the filtering lview of listview. cleartextfilter ();} else {// use the content entered by the user to filter the list items of the listview lview. setfiltertext (newtext);} return true;} // method triggered when you click the search button @ override public Boolean onquerytextsubmit (string qurery) {// This is just a simulation, in practice, you should query the method and get the result toast. maketext (mainactivity. this, "what you choose is:" + qurery, 0 ). show (); Return false;} private void viewinit () {sview = (searchview) findviewbyid (R. id. searchview_id); lview = (listview) findviewbyid (R. id. listview_id );}}

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.