Android SearchView的使用方法
2014-09-04 20:29
274 查看
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() { @Override public boolean onQueryTextSubmit(String query) { Log.d(TAG, "onQueryTextSubmit(), query=" + query); return false; } @Override public 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); }
相关文章推荐
- Android搜索框组件SearchView的基本使用方法
- android webView 使用方法
- android webView的使用方法
- Android 非UI线程使用View.post()方法一处潜在的内存泄漏
- android上不使用qq的SDK,用WebView登陆qq的方法。
- Android提高21篇之二:SurfaceView的基本使用方法
- Android编程 - WebView控件基本使用方法
- Android中AutoCompleteTextView的使用方法
- 【Android】使用SearchView时软键盘不支持actionSearch的问题
- android开发android webView 使用方法
- 【Android 开发】:UI控件之显示图片控件 ImageView 的使用方法
- Android google map 开发中使用mapview产生INSTALL_FAILED_MISSING_SHARED_LIBRARY错误的解决方法
- 【Android动态布局】之【使用addView方法时,如何保持已有动态控件位置不被改变】
- android开发之android webView使用方法
- 《转载》Android AlertDialog 方法setView(view,0,0,0,0)的使用
- android webView 使用方法
- Android使用原生组件WebView加载网页和数据的方法
- android webView使用方法二
- android使用videoview 全屏方法
- 【Android】使用SearchView时软键盘不支持actionSearch的问题