搜索框(SearchView)详解
来源:互联网 发布:软件售后服务计划书 编辑:程序博客网 时间:2024/04/29 15:40
搜索框(SearchView)详解
ONE Goal, ONE Passion !
整理一下一些系统常用的小组件.先说说搜索框吧.
- SearchView是 搜索框组件,它可以让用户在文本框内输入文字,并允许通过监听器监听用户输入,当用户输入完成后提交搜索时也可以执行实际的搜索.
第1步: 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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.windowtest1.SearchViewActivity"> <SearchView android:id="@+id/sv" android:layout_width="match_parent" android:layout_height="wrap_content" /><--可以为SearchView增加自动完成的功能 --> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>
第2步: 获得SearchView并做一些处理
public class SearchViewActivity extends Activity { private SearchView sv; private ListView lv; private String[] mStrings = {"123", "456", "789"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_view); lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, mStrings)); //设置ListView启动过滤 lv.setTextFilterEnabled(true); sv = (SearchView) findViewById(R.id.sv); //设置sv默认是否自动缩小为图标 sv.setIconifiedByDefault(false); //设置显示收索按钮 sv.setSubmitButtonEnabled(true); //设置sv默认显示的提示文本 sv.setQueryHint("查找"); //为该sv设置监听 sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { //用户点击输入时触发 @Override public boolean onQueryTextSubmit(String query) { System.out.println("选择的是---" + query);// 一般实际应用中可在这里做逻辑处理 return false; } // 用户输入时触发 @Override public boolean onQueryTextChange(String newText) { //如果newText不为0 if (newText.isEmpty()) { // lv.clearTextFilter(); } else { //使用用户输入内容对lv进行过滤 lv.setFilterText(newText); } return false; } }); }}
需要注意的问题—– 为ListView填充数据时使用ArrayAdapter可行,但是如果使用BaseAdapter是不可以实现提示功能的(也就是listview没有作用了).所以还是不要使用BaseAdapter.
市面上见的搜索框比这复杂一些,如网易的新闻搜索,不能使用简单的listview去更改listview是否过滤.要这样实现
伪代码:
1,使用EditView充当输入框,并监听输入内容. 2,获得输入的内容后联网去查询应该提示给用户的信息. 3.当用户点击某一条信息时,实际上点击的是lisetview中某个item.我们 获得这个item的内容,然后将它显示到EditView中.点击搜索 再去做逻辑处理即可.
要下班了,有时间再补充.实现起来应该是满简单的.
0 0
- 搜索框(SearchView)详解
- SearchView搜索框架详解
- SearchView搜索框
- SearchView搜索框
- SearchView--搜索框案例
- 自定义搜索框searchview
- Android 搜索框:SearchView 的属性和用法详解
- Android 搜索框:SearchView 的属性和用法详解
- Android 搜索框:SearchView 的属性和用法详解
- 自定义搜索框(SearchView)
- 修改searchview搜索框光标
- 搜索框—— SearchView
- 搜索SearchView
- SearchView---搜索
- 自定义超简单SearchView搜索框
- 【转】自定义搜索框(SearchView)
- Android学习笔记(29):搜索框SearchView
- <SearchView > 搜索框的简单使用
- 自定义titlebar
- 安卓开发环境配置相关问题(坚果手机)
- XML和Schema命名空间详解---实例篇
- 第五章.简单的数据查询.总结
- 【eralng】random 伪随机
- 搜索框(SearchView)详解
- Deep Learning及NLP(自然语言处理)杂谈--第一部分
- SpringMVC--常用注解(详解)
- string.h
- -Dmaven.multiModuleProjectDirectory system propery is not set.
- Html5添加SVG和Raphaël.js的混合图标动画特效插件教程
- R 和 R-Studio安装
- 异常出现及其解决
- Windows Server 2008 R2终端服务器远程授权激活