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




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android SearchView 是一个内置的 Android 搜索框,可以用来处理搜索操作。它通常用于在应用程序中添加搜索功能,这样用户就可以搜索并过滤内容。 SearchView 可以在 ActionBar、Toolbar 或任何其他视图中使用。它提供了以下功能: 1. 搜索查询输入框 2. 搜索图标按钮 3. 可以自定义的搜索提示 4. 可以通过监听器处理搜索操作 5. 可以通过搜索框内的 X 按钮清除搜索查询 6. 可以通过监听器处理搜索框焦点变化 可以使用以下步骤在应用程序中添加 SearchView: 1. 在布局文件中添加 SearchView。 2. 在 Activity 或 Fragment 中获取对 SearchView 的引用。 3. 设置 SearchView 的属性和监听器。 4. 处理搜索操作。 下面是一个简单的示例: 1. 在布局文件中添加 SearchView。 ``` <SearchView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" android:iconifiedByDefault="false" android:queryHint="Search..."> </SearchView> ``` 2. 在 Activity 或 Fragment 中获取对 SearchView 的引用。 ``` SearchView searchView = findViewById(R.id.searchView); ``` 3. 设置 SearchView 的属性和监听器。 ``` searchView.setQueryHint("Search..."); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { // 处理搜索操作 return false; } @Override public boolean onQueryTextChange(String newText) { // 处理搜索框文本变化 return false; } }); ``` 4. 处理搜索操作。 在 onQueryTextSubmit() 方法中处理搜索操作。 ``` @Override public boolean onQueryTextSubmit(String query) { // 处理搜索操作 search(query); return false; } private void search(String query) { // 处理搜索查询 } ``` 这是一个简单的示例,您可以根据需要自定义 SearchView 的行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值