Android SearchView的使用方法

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);
    }




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值