Android SearchView的使用方法
来源:互联网 发布:广电网络机顶盒 编辑:程序博客网 时间:2024/05/18 13:07
1. 概述
要实现搜索,就会用到SearchView,本文简单介绍SearchView的使用方法。
要点:
- 在布局中定义SearchView
- 注册SearchView的一些listener
2. 示例
下面是示例的代码。
布局文件:
<?xml version="1.0" encoding="utf-8"?><!-- hello_search_view.xml --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/search_view_example"/> <SearchView android:id="@+id/search_view_id" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
用到的字符串资源:
<string name="search_view_example">Search View Example</string>
Java代码:
package com.example.hellosearchview;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.SearchView;import android.widget.SearchView.OnQueryTextListener;public class MainActivity extends Activity {protected static final String TAG = "MainActivity";private SearchView searchView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.hello_search_view); searchView = (SearchView) this.findViewById(R.id.search_view_id); searchView.setOnQueryTextListener(new OnQueryTextListener() {@Overridepublic boolean onQueryTextSubmit(String query) {Log.d(TAG, "onQueryTextSubmit(), query=" + query);return false;}@Overridepublic boolean onQueryTextChange(String newText) {Log.d(TAG, "onQueryTextChange(), newText=" + newText);return false;} }); searchView.setSubmitButtonEnabled(true); }}
效果:
在搜索框键入文本时,Android会回调onQueryTextChange()方法;输入键盘的回车键或者搜索框最后边的箭头,就会提交一次查询,即会回调onQueryTextSubmit()方法。
3. submit button
上面的代码启用了SearchView的submit button:
searchView.setSubmitButtonEnabled(true);
所以搜索框右边有个小箭头,输入搜索文本之后,单击这个submit button就提交了搜索。
因为这个submit button会占用UI空间,因此很多情况下都不会显示这个按钮,而直接通过键盘的回车键来实现同样的功能。要去掉这个按钮,只需要设置为false即可:
searchView.setSubmitButtonEnabled(false);
效果如下:
4. 另一些常用方法
searchView.setIconifiedByDefault(true);searchView.setIconified(false);searchView.setQueryHint("Input some text");
5. 附 SearchView.OnQueryTextListener的源码
/** * Callbacks for changes to the query text. */ public interface OnQueryTextListener { /** * Called when the user submits the query. This could be due to a key press on the * keyboard or due to pressing a submit button. * The listener can override the standard behavior by returning true * to indicate that it has handled the submit request. Otherwise return false to * let the SearchView handle the submission by launching any associated intent. * * @param query the query text that is to be submitted * * @return true if the query has been handled by the listener, false to let the * SearchView perform the default action. */ boolean onQueryTextSubmit(String query); /** * Called when the query text is changed by the user. * * @param newText the new content of the query text field. * * @return false if the SearchView should perform the default action of showing any * suggestions if available, true if the action was handled by the listener. */ boolean onQueryTextChange(String newText); }
0 0
- Android SearchView的使用方法
- Android SearchView的使用
- 关于android SearchView的使用
- android SearchView
- Android SearchView
- android SearchView
- Android搜索控件SearchView的用法
- android toolbar中searchView的使用
- Android中SearchView的功能和用法
- android:ToolBar和SearchView的使用总结
- Android沉浸式与SearchView的坑
- android v7.widget.SearchView的使用
- android中SearchView的简单使用
- Android EditText达到SearchView的效果
- Android 之SearchView翻译
- Android 细说searchView
- android SearchView 样式修改
- android之SearchView
- iOS 真机不能显示图片问题
- UVA - 10288 Coupons (概率+递推)
- 【坑】javascript中的prototype
- volatile关键字
- 用两个栈实现一个队列
- Android SearchView的使用方法
- 进程管理之schedule()
- USACO Cow Tour
- web服务器【apache/nginx] 关闭目录的浏览权限
- [LeetCode] Next Permutation
- 堆排序
- Java基础10天 学习笔记_面向对象(异常 finally关键字)
- java多态
- [ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)