码迷,mamicode.com
首页 > 其他好文 > 详细

SearchView--------搜索功能-------------------------

时间:2016-10-04 18:16:42      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

xml---------------xml布局-------------------------

 

<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<SearchView
android:id="@+id/sv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:targetApi="honeycomb"></SearchView>

<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

</ListView>
</LinearLayout>



MainActivity-----------------------------调用-------------------------

private SearchView searchView;
private ListView listView;
private final String[] mStrings={
"aaaaa",
"bbbbb",
"ccccc",
};
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate((saveInstanceState));
setContentView(R.layout.searchview);
listView=(ListView) findViewById(R.id.lv);
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mStrings));
//设置ListView启动过滤
listView.setTextFilterEnabled(true);

searchView=(SearchView) findViewById(R.id.sv);
//设置默认是否缩小为图标
searchView.setIconifiedByDefault(false);
//设置是否显示搜索按钮
searchView.setSubmitButtonEnabled(true);
//设置sv内的默认显示的提示文本
searchView.setQueryHint("查找");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
if(TextUtils.isEmpty(newText)){
listView.clearTextFilter();
}
else {
listView.setFilterText(newText);
}
return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(MainActivity.this,"您的选择是:"+query,Toast.LENGTH_SHORT).show();
return false;
}


});
}

SearchView--------搜索功能-------------------------

标签:

原文地址:http://www.cnblogs.com/ZeroMurder/p/5930611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!