Searchview Introduction
Searchview is a search box component that allows users to search for text and then display it. ‘
code example
This example adds a dress moth listview to add auto-completion to the Searchview.
PackagePeng.liu.test;Importandroid.app.Activity;ImportAndroid.graphics.Color;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;ImportAndroid.text.TextUtils;Importandroid.view.Gravity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.Button;ImportAndroid.widget.CalendarView;ImportAndroid.widget.DatePicker;ImportAndroid.widget.EditText;ImportAndroid.widget.FrameLayout;ImportAndroid.widget.GridView;ImportAndroid.widget.ImageSwitcher;ImportAndroid.widget.ImageView;ImportAndroid.widget.LinearLayout;ImportAndroid.widget.ListView;ImportAndroid.widget.NumberPicker;ImportAndroid.widget.SearchView;ImportAndroid.widget.SimpleAdapter;ImportAndroid.widget.TextSwitcher;ImportAndroid.widget.TextView;ImportAndroid.widget.TimePicker;ImportAndroid.widget.Toast;ImportAndroid.widget.ViewFlipper;ImportAndroid.widget.ViewSwitcher;ImportJava.util.ArrayList;ImportJava.util.Calendar;ImportJava.util.HashMap;ImportJava.util.List;ImportJava.util.Map; Public class mainactivity extends Activity implements Searchview . Onquerytextlistener { Privatestring[] STRs =Newstring[]{"Java","Python","HTML"};PrivateListView list;PrivateSearchview Searchview;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); List = (ListView) Findviewbyid (r.id.list); List.setadapter (NewArrayadapter<string > ( ThisAndroid. R.layout.simple_list_item_1,strs)); List.settextfilterenabled (true); Searchview = (Searchview) Findviewbyid (R.ID.SV); Searchview.seticonifiedbydefault (false); Searchview.setonquerytextlistener ( This); Searchview.setsubmitbuttonenabled (true); Searchview.setqueryhint ("Find"); }@Override Public Boolean Onquerytextsubmit(String s) {Toast.maketext ( This,"Search for Characters"+s,toast.length_long). Show ();return false; }@Override Public Boolean Onquerytextchange(String s) {if(Textutils.isempty (s)) {List.cleartextfilter (); }Else{List.setfiltertext (s); }return true; }}
<?xml version= "1.0" encoding= "Utf-8"?><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:paddingleft="@dimen/activity_horizontal_margin"Android:paddingright="@dimen/activity_horizontal_margin"Android:paddingtop="@dimen/activity_vertical_margin"Android:paddingbottom="@dimen/activity_vertical_margin"Tools:context=". Mainactivity "android:orientation="Vertical"> <searchviewandroid:layout_width="Wrap_content"android:layout_height ="Wrap_content"android:id="@+id/sv"/> <ListViewandroid:layout_width="Match_parent"android:layout_height= "0DP" android:id="@+id/list"android:layout_weight="1" /> </linearlayout>
Android search box Searchview usage-android learning Journey (39)