Activity_main.xml
<Relativelayoutxmlns: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"Tools:context= "${relativepackage}.${activityclass}" > <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"android:orientation= "vertical" > <SearchviewAndroid:id= "@+id/searchview1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > </Searchview> <ListViewAndroid:id= "@+id/listview1"Android:layout_width= "Match_parent"Android:background= "#888"Android:layout_height= "Wrap_content" > </ListView> </LinearLayout></Relativelayout>
View Code
Mainactivity.java
Public classMainactivityextendsActivity {Searchview searchView1; ListView ListView1; string[] Items=Newstring[]{"Aaaaaaaaa", "bbbbbbbbb", "CCCCCCCCCC", "AA", "BB", "CC" }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); SearchView1=(Searchview) Findviewbyid (R.ID.SEARCHVIEW1); //Show Submit ButtonSearchview1.setsubmitbuttonenabled (true); //set to fetch is hidden to show only one iconSearchview1.seticonifiedbydefault (false); //hint hintSearchview1.setqueryhint ("Please enter filter conditions"); ListView1=(ListView) Findviewbyid (R.ID.LISTVIEW1); //binding a data setListview1.setadapter (NewArrayadapter<string>(Getapplicationcontext (), Android. R.layout.simple_list_item_1,items)); //set Allow filteringListview1.settextfilterenabled (true); //Increase EventSearchview1.setonquerytextlistener (NewOnquerytextlistener () {@Override Public Booleanonquerytextsubmit (String query) {toast.maketext (Getapplicationcontext (),"You are inquiring:" +query, Toast.length_short). Show (); return true; } @Override Public BooleanOnquerytextchange (String newtext) {if(Textutils.isempty (NewText)) {listview1.cleartextfilter (); }Else{listview1.setfiltertext (NewText); } return true; } }); }}
View Code
Android Learning-Interface-ui-searchview