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 );}}