现在的位置: 首页 > 综合 > 正文

Android开发之SearchView

2017年02月17日 ⁄ 综合 ⁄ 共 2855字 ⁄ 字号 评论关闭

SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索。

    使用SearchView时可以使用如下常用方法。

  • setIconifiedByDefault(boolean iconified):设置该搜索框默认是否自动缩小为图标。
  • setSubmitButtonEnabled(boolean enabled):设置是否显示搜索按钮。
  • setQueryHint(CharSequence hint):设置搜索框内默认显示的提示文本。
  • setOnQueryTextListener(SearchView.OnQueryTextListener listener):为该搜索框设置事件监听器。

    如果为SearchView增加一个配套的ListView,则可以为SearchView增加自动完成的功能。如下实例示范了SearchView的功能与用法。

    实例:搜索   

    该实例的界面布局文件中定义了一个SearchView和ListView,其中ListView用于为SearchView显示自动补齐列表。界面布局文件如下。

    

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
   <!-- 定义一个SearchView -->
   <SearchView android:id="@+id/sv"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
   <!-- 为SearchView定义自动完成的ListView -->
   <ListView android:id="@+id/lv"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
      />
</LinearLayout>
复制代码

      上面的布局文件中定义了一个SearchView组件,并为该SearchView组件定义了一个ListView组件,该ListView组件用于为SearchView组件显示自动完成列表。

      下面是该实例对应的Activity代码。

      该Activity对应的后台代码文件如下:

    

复制代码
package org.crazyit.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.widget.*;

public class SearchViewTest extends Activity implements SearchView.OnQueryTextListener {

    
    private SearchView sv;
    private ListView lv;
    //自动完成的列表
    private final String[] mStrings={"aaaaaa","bbbbbb","cccccc"};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_view_test);
        lv=(ListView)findViewById(R.id.lv);
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mStrings));
        lv.setTextFilterEnabled(true);
        
        sv=(SearchView)findViewById(R.id.sv);
        //设置该SearchView默认是否自动缩小为图标
        sv.setIconifiedByDefault(false);
        //为该SearchView组件设置事件监听器
        sv.setOnQueryTextListener(this);
        //设置该SearchView显示搜索按钮
        sv.setSubmitButtonEnabled(true);

        //设置该SearchView内默认显示的提示文本
        sv.setQueryHint("查找");
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_view_test, menu);
        return true;
    }
    //用户输入字符时激发该方法
    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        if(TextUtils.isEmpty(newText))
        {
            //清楚ListView的过滤
            lv.clearTextFilter();
        }
        else
        {
            //使用用户输入的内容对ListView的列表项进行过滤
            lv.setFilterText(newText);
        
        }
        return true;
    }
     //单击搜索按钮时激发该方法
    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        //实际应用中应该在该方法内执行实际查询
        //此处仅使用Toast显示用户输入的查询内容
    Toast.makeText(this, "您选择的是:"+query, Toast.LENGTH_SHORT).show();
        return true;
    }

}
复制代码

上面的程序中粗体字代码就是控制SearchView的关键代码,第一段粗体字代码我iSearchView设置了事件监听器,并为该SearchView启用了搜索按钮。接下来程序重写了onQueryTextChange()、onQueryTextSubmit()两个方法,这两个方法用于为SearchView的事件提供响应。

    运行上面的程序,将看到如下效果:

抱歉!评论已关闭.