activity_main.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<RelativeLayout 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" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="vertical" > <SearchView android:id="@+id/searchView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </SearchView> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:background="#888" android:layout_height="wrap_content" > </ListView> </LinearLayout> </RelativeLayout>
MainActivity.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class MainActivity extends Activity { SearchView searchView1; ListView listView1; String[] items=new String[]{ "aaaaaaaaa","bbbbbbbbb","cccccccccc", "aa","bb","cc" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); searchView1=(SearchView)findViewById(R.id.searchView1); //显示提交按钮 searchView1.setSubmitButtonEnabled(true); //设置为取则隐藏为只显示一个图标 searchView1.setIconifiedByDefault(false); //提示hint searchView1.setQueryHint("请输入过滤条件"); listView1=(ListView)findViewById(R.id.listView1); //绑定数据集 listView1.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,items)); //设置允许过滤 listView1.setTextFilterEnabled(true); //增加事件 searchView1.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { Toast.makeText(getApplicationContext(), "你查询的是:"+query, Toast.LENGTH_SHORT).show(); return true; } @Override public boolean onQueryTextChange(String newText) { if(TextUtils.isEmpty(newText)){ listView1.clearTextFilter(); }else{ listView1.setFilterText(newText); } return true; } }); } }