您的位置:首页 > 移动开发 > Android开发

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: