Android SearchView搜索框组件的使用方法
作者:lijiao 发布时间:2023-06-25 08:13:42
标签:Android,SearchView,搜索框,组件
SearchView是搜索框组件,它可以让用户在文本框里输入文字,通过 * 取得用户的输入,当用户点击搜索时, * 执行实际的搜索。
本文就为大家分享了SearchView搜索框组件的使用方法,供大家参考,具体内容如下
效果:
代码SearchActivity.java
package com.jialianjia.bzw.activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.annotation.SuppressLint;
import android.widget.ListView;
import android.widget.SearchView;
import com.jialianjia.bzw.BaseActivity;
import com.jialianjia.bzw.R;
import com.lidroid.xutils.ViewUtils;
import java.util.ArrayList;
/**
* 搜索
* Created by Gxs on 2016/5/5.
*/
public class SearchActivity extends BaseActivity implements SearchView.OnQueryTextListener{
private SearchView searchView;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> arrayList = new ArrayList<String>();
private Object[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
names = loadData();
ViewUtils.inject(this);
searchView = (SearchView) findViewById(R.id.searchView);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
android.R.layout.simple_expandable_list_item_1, names));
searchView.setOnQueryTextListener(this);
searchView.setSubmitButtonEnabled(false);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Object[] obj = searchItem(newText);
updateLayout(obj);
return false;
}
public Object[] searchItem(String name) {
ArrayList<String> mSearchList = new ArrayList<String>();
for (int i = 0; i < arrayList.size(); i++) {
int index = arrayList.get(i).indexOf(name);
// 存在匹配的数据
if (index != -1) {
mSearchList.add(arrayList.get(i));
}
}
return mSearchList.toArray();
}
// 更新数据
public void updateLayout(Object[] obj) {
listView.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
android.R.layout.simple_expandable_list_item_1, obj));
}
// 测试数据
public Object[] loadData() {
arrayList.add("aaa");
arrayList.add("aab");
arrayList.add("aac");
arrayList.add("aad");
arrayList.add("abc");
arrayList.add("abcd");
arrayList.add("cdf");
arrayList.add("eda");
arrayList.add("sdfa");
arrayList.add("ddda");
arrayList.add("sssa");
return arrayList.toArray();
}
}
布局activity_search.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="?attr/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:background="@drawable/shape_search"
android:queryHint="请输入您要查找的内容"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"/>
</LinearLayout>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"></TableRow>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#969696"/>
</LinearLayout>
大家还可以参考:Android搜索框组件SearchView的基本使用方法 进行深入学习。


猜你喜欢
- 最近在工作中处理了一些内存泄露的问题,在这个过程中我尤其发现了一些基本的问题反而忽略导致内存泄露,比如静态变量,cursor关闭,线程,定时
- 本文实例讲述了Android编程之SMS读取短信并保存到SQLite的方法。分享给大家供大家参考,具体如下:Android 之 SMS 短信
- 先准备好一个新闻实体类package com.zb.fragmentbestpractice/** * title:表示新闻的实体类 * c
- 在正常开发中,我们客户端需要用webView加载网页,再遇到网络慢或者访问的服务器响应时,页面是空白的,所以为了用户更好的体验,我们可以提供
- 在实际开发中经常需要了解具体对象的类型,所以经常会使用GetType()和typeof()、尽管可以得到相应的类型、但两者之间也存在一些差别
- 本文实例为大家分享了android自定义View实现五子棋的具体代码,供大家参考,具体内容如下先说一下吧,android的自定义View就是
- 前言本文主要给大家介绍了关于spring mvc注解@ModelAttribute妙用的相关内容,分享出来供大家参考学习,下面话不多说了,来
- TCP异步Socket模型C#的TCP异步Socket模型是通过Begin-End模式实现的。例如提供BeginConnect、BeginA
- 本文实例为大家分享了java自定义异常打印内容的具体代码,供大家参考,具体内容如下背景:在开发中,我们可能会使用到自定义异常,但是,这个自定
- 前言在前后端分离开发的项目当中为了避免重复构建发布,我们需要部署一个持续发布环境,而目前的开发环境服务器都是基于 CentOS 的,因此每次
- 本文实例讲述了Android实现EditText中添加和删除bitmap的方法。分享给大家供大家参考,具体如下:SpannableStrin
- 线程间通信我们看下面的图我们来看线程间通信的原理:线程(Thread B)和线程(Thread A)通信, 首先线程A 必须实现同步上下文对
- 使用 Spring 时,XML 和注解是使用得最多的两种配置方式,虽然是两种完全不同的配置方式,但对于 IOC 容器来说,两种方式的不同主要
- SwipeRefresh基于原生的SwipeRefreshLayout 做了封装处理此项目中包括种:1.原生SwipeRefreshLayo
- 本文实例分析了Android编程画图之抗锯齿解决方法。分享给大家供大家参考,具体如下:在画图的时候,图片如果旋转或缩放之后,总是会出现那些华
- 重写addResourceHandlers映射文件路径在看一个博客源码发现页面的图片所映射的地址在SpringBoot静态资源文件夹下找不到
- 前言自从用了SpringBoot,个人最喜欢的就是SpringBoot的配置文件了,和Spring比起SpringBoot更加灵活,修改的某
- 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using System
- 移除一段文字中的HTML标记,以消除其中包含的样式和段落等,最常用的办法可能就是正则表达式了。但是请注意,正则表达式并不能处理所有的HTML
- 1.多节点无缝切换问题分布式节点中的服务宕机或者重启不影响客户端使用分布式节点中的服务宕机重启不影响业务服务内部通信如果在某个分布式系统中想