使用EditText+ListView并结合TextWatcher实现输入关键字筛选数据

article/2025/10/28 5:30:46

    想必大家应该遇到过这样的情况,当点击Spinner控件后弹出的列表内容超多,一个一个滑动着去寻找所要的项很麻烦,尤其是当自己知道想要选择的内容,这时候如果我们只需要输入某些关键字,就可以讲上百条数据筛选出几十条甚至更少,岂不是会方便很多。

    其实这是项目中的一个需求,由于目前公司接触的多数和数据采集相关的PDA项目,有很多填写项一个spinner已经不方便满足需求,虽然客户还没有提出,但提前做好优化总是没有错的,所以项目组的同事提出这个需求并让我尝试着去做出来,当中给了我不少帮助和意见。

    闲言少叙,简单说下这个demo的实现,点击一个button,弹出一个类似spinner的界面,包含一个edittext和listview,当在输入框中键入关键字时,下面的listview所显示的数据可以随之进行筛选,点击item,将所选值返回给button。实现其实不难,只不过接触了一个新的知识而已,就是TextWatcher,它本身是一个接口,需要实现并覆盖它的三个方法,在每个方法中执行相应的操作,然后在需要的控件上添加监听即可。先来看本Demo实现后的效果

                                                                                             

                                               进入后点击按钮                                                                                                    弹出数据界面

 

                                                                                             

                                             输入关键字进行筛选                                                                                               点击子项目返回给按钮

 

以下是部分代码的实现,其实比较简单,唯一不太熟悉的就是TextWatcher,因为之前没用过,但是很简单,只有三个方法,现实了就OK了

 

        首先介绍一下这个自己写的类,它实现了一个数据的值value和显示名称Name的绑定,可以很方便的用于添加数据,当然也可以使用自己的方法去添加数据,本例子我就用这个了。

package com.cogent.enumbutton;
/**
* 一个Value(绑定值)-Name(显示名称)对象,如:1-汉族
*/
public class ValueNameDomain {
private String Value;//绑定的值
private String Name;//显示的选项名称
public ValueNameDomain(){}
public ValueNameDomain(String name,String value){
this.Name = name;
this.Value = value;
}
/**
* 获取绑定的值
*/
public String getValue() {
return Value;
}
/**
* 设置绑定的值
*/
public void setValue(String value) {
this.Value = value;
}
/**
* 获取显示的选项名称
*/
public String getName() {
return Name;
}
/**
* 设置显示的选项名称
*/
public void setName(String name) {
this.Name = name;
}
@Override
public String toString() {
return Name;
}
}


 

 

这个是demo的关键了,运用一个窗口样式的activity实现类似spinner的功能,具体的地方我都进行了自己能看懂的注解,对输入控件添加addTextChangedListener,并实现其中的三个方法就完成了,三个方法比较简单,为别

也就是文字发生改变之前,改变时,和改变之后进行相应的操作,看看SDK就神马都解决了

 

package com.cogent.enumbutton;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class EditTextListView extends Activity {
//按钮静态缓存,该用法可以避免使用startActivityForResult来获取按钮返回的时间
public static Button btn;
private EditText edit_search;
private ListView lv;
private EditTextListViewAdapter adapter;
List<ValueNameDomain> list = new ArrayList<ValueNameDomain>();//所有的数据list
List<ValueNameDomain> newlist = new ArrayList<ValueNameDomain>();//查询后的数据list
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.edittextlistview);
init();
initDefaultLists();
}
//初始化控件
private void init() {
edit_search = (EditText) findViewById(R.id.edit_search);
//为输入添加TextWatcher监听文字的变化
edit_search.addTextChangedListener(new TextWatcher_Enum());
adapter = new EditTextListViewAdapter(this, list);
lv = (ListView) findViewById(R.id.edittextListview);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new onclick());
}
//添加数据
private void initDefaultLists() {
ValueNameDomain domain = new ValueNameDomain();
for (int i = 1; i <= 20; i++) {
domain = new ValueNameDomain();
domain.setName("测试数据" + i);
domain.setValue(i + "");
list.add(domain);
}
}
//当editetext变化时调用的方法,来判断所输入是否包含在所属数据中
private List<ValueNameDomain> getNewData(String input_info) {
//遍历list
for (int i = 0; i < list.size(); i++) {
ValueNameDomain domain = list.get(i);
//如果遍历到的名字包含所输入字符串
if (domain.getName().contains(input_info)) {
//将遍历到的元素重新组成一个list
ValueNameDomain domain2 = new ValueNameDomain();
domain2.setName(domain.getName());
domain2.setValue(i + "");
newlist.add(domain2);
}
}
return newlist;
}
//button的点击事件
class onclick implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
TextView text = (TextView) view.findViewById(R.id.tvData);
String str = (String) text.getText();
btn.setText(str);
EditTextListView.this.finish();
}
}
//TextWatcher接口
class TextWatcher_Enum implements TextWatcher {
//文字变化前
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
//文字变化时
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
newlist.clear();
if (edit_search.getText() != null) {
String input_info = edit_search.getText().toString();
newlist = getNewData(input_info);
adapter = new EditTextListViewAdapter(EditTextListView.this,
newlist);
lv.setAdapter(adapter);
}
}
//文字变化后
@Override
public void afterTextChanged(Editable s) {
}
}
}


这是个比较实用的demo,打算进一步整理出来封装起来,以后项目要是用到的话就可以直接拿来用啦,结尾附上源码,希望大家可以一起学习分享,坚持记录自己的android路程。


http://chatgpt.dhexx.cn/article/rSZKuBLS.shtml

相关文章

AutoCompleteTextView与TextWatcher的结合

/******************************************************************************************** * author&#xff1a;conowen大钟 * E-mail&#xff1a;conowenhotmail.com * http://blog.csdn.net/conowen * 注&#xff1a;本文为原创&#xff0c;仅作为学习交流使用&…

使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听

之前博客上的有关EditText的文章&#xff0c;只是介绍EditText的一些最基本的用法&#xff0c;这次来深入学习一下EditText。 监听EditText的变化 使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听&#xff0c;TextWatcher是一个接口类&#…

TextView

文本控件 显示富文本&#xff08;URL、不同大小、字体、颜色的文本&#xff09; 在TextView中预定义了一些类似HTML标签&#xff08;不区分大小写&#xff09;&#xff0c;通过这些标签&#xff0c;我们可以使TextView控件显示不同的颜色、大小、字体的文字。 常见的标签如下…

Android TextWatcher监控EditText,TextView

布局中EditText在android布局中经常用到&#xff0c;对EditText中输入的内容也经常需要进行限制&#xff0c;我们可以通过TextWatcher去观察输入框中输入的内容&#xff0c;作个笔记。 主布局&#xff1a; <?xml version"1.0" encoding"utf-8"?>&…

使用TextWatcher监听EditText的文本变化之后动态改变EditText里面的内容

今天碰到一个这样的问题&#xff1a;使用TextWatcher对EditText进行监听&#xff0c;当EditText中值为某一特定内容时&#xff0c;将EditText中的值设为我们需要的那个值。一开始就是简单的在EditText中使用setText(...)来实现&#xff0c;结果出现栈溢出的异常。这里提供一个解…

详解EditText输入监听TextWatcher

日常开发中&#xff0c;我们可能会遇到需要监听EditText输入&#xff0c;比如判断输入是否为电话号码&#xff0c;获取输入的数据长度来限定字数等。这就需要监听EditText的输入状态。EditText使用TextWatcher实现类似按钮监听事件&#xff1a; 使用方法 效果图&#xff1a; …

Android TextWatcher三个回调详解,监听EditText的输入

TextWatcher是一个监听字符变化的类。当我们调用EditText的addTextChangedListener(TextWatcher)方法之后&#xff0c;就可以监听EditText的输入了。 在new出一个TextWatcher之后&#xff0c;我们需要实现三个抽象方法&#xff1a; beforeTextChangedonTextChangedafterTextCh…

android TextWatcher 学习

1.简介 主要用来监听用户输入&#xff0c;然后剪裁输入。 比如输入框只能输入8个字节的内容&#xff0c;就可以用TextWatcher来实现。 public interface TextWatcher extends NoCopySpan {/*** This method is called to notify you that, within <code>s</code>…

android的TextView的TextWatcher使用

TextWatcher是一个文本变化监听接口&#xff0c;定义了三个接口&#xff0c;分别是beforeTextChanged,onTextChanged,afterTextCahnged. TextWatcher通常与TextView结合使用&#xff0c;以便在文本变化的不同时机做响应的处理。TextWatcher中三个回调接口都是使用了InputFilter…

Android 文本监听接口TextWatcher详解

TextWatcher是一个用来监听文本变化的接口&#xff0c;使用该接口可以很方便的对可显示文本控件和可编辑文本控件中的文字进行监听和修改 TextWatcher接口中定义了三个方法&#xff1a; public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 该…

makefile中的两个函数(wildcard和patsubst)

(1) wildcard函数 作用是查找指定目录下指定类型的文件&#xff0c;并最终返回一个环境变量&#xff0c;需要用$取值赋值给另一个环境变量&#xff01;该函数只有一个参数&#xff0c;如取出当前目录下的所有.c文件&#xff0c;并赋值给allc普通变量&#xff1a; allc$(wildc…

linux_makefile文件编写,基本规则、工作原理、模式规则,wildcard函数、patsubst函数

接上一篇&#xff1a;linux_GDB调试学习(调试运行、多文件设置断点)_C/C程序调试 本次来分享linux下的makefile文件的编写&#xff0c;开始上菜&#xff1a; 目录 1.makefile文件的命名规则2.用途3.基本规则3.1.用例一4.工作原理4.1.用例二 5.makefile的执行5.1.用例三 6.make…

makefile wildcard patsubst使用小结

这是当前的目录&#xff1a; objs main.o g_a.o g_b. target : $(objs)gcc -o target $(objs) main.o : g_a.h g_b.hgcc -c main.c g_a.o : g_a.hgcc -c g_a.c g_b.o : g_b.hgcc -c g_b.c.PHONY : cleansrc $(wildcard *.c) obj $(patsubst %.c,%.o,$(src)) #obj $(patsu…

Makefile中patsubst函数使用方法

Makefile中patsubst函数使用方法 patsubst函数用于将文件模式进行替换。 一、作用 替换文件后缀。 二、格式 $(patsubst 原模式&#xff0c; 目标模式&#xff0c; 文件列表) 三、实例 图1 源文件结构 图2 patsubst实例

从零开始学习makefile(5)makefile中patsubst的作用

目录 介绍 text pattern与replacement 返回值 通配符% 示例1 例子2 介绍 patsubst是pattern substitute的缩写。其用法是&#xff1a; $(patsubst pattern,replacement,text) text text是将要被处理的字符串。首先&#xff0c;patsubst以空格为分隔符&#xff0c;将te…

list_for_each_entry和list_for_each_entry_safe

看内核代码都会发现&#xff0c;内核链表的操作常用的二个宏list_for_each_entry和list_for_each_entry_safe 循序渐进&#xff0c;先从最底层的函数container_of函数说起&#xff0c;其内核定义如下&#xff1a; 先看offsetof宏&#xff0c;根据优先级的顺序&#xff0c;最里面…

Map中的entry,entrySet,keySet的区别和用法

Map中的元素是以键值对的形式存在的&#xff0c;即key-value。key是唯一的不能重复&#xff0c;但value可以重复。 Map.keySet(): 这个方法返回的就是map集合中所有键Key的一个Set集合。如Map<Integer&#xff0c;String> 中put(1, “张三”)&#xff0c;put(2, “李四”…

Map.Entry与entrySet与entry,getKey()与entry.getValue()的用法

直接上代码 实体类 Data AllArgsConstructor NoArgsConstructor public class SinglePressureResultDTO {private Integer Times; private Integer SCU_number; private Boolean Intervention; private Long startTime_low; private Long low_time; private Long start…

Map集合的entrySet()方法

之前学习集合的时候要通过迭代器来迭代的时候最难得就是map集合得迭代&#xff0c;一直也不太明白&#xff0c;今天总算搞懂了 首先我们看什么容器才能迭代 根据API我们得知是对所有collection迭代的集合&#xff0c;那么已知的collection容器有哪些 我把常用的标出了&#xf…

keySet()和entrySet()

一、描述 keySet()和entrySet()&#xff0c;是Map集合中的两种取值方法。 与get(Object key)相比&#xff0c;get(Object key)只能返回到指定键所映射的值&#xff0c;不能一次全部取出。而keySet()和entrySet()可以。 Map集合中没有迭代器&#xff0c;Map集合取出键值的原理…