安卓:addTextChangedListener工作不正常

编程入门 行业动态 更新时间:2024-10-10 04:28:41
本文介绍了安卓:addTextChangedListener工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在用户打字,所以我使用了 addTextChangedListener 方法来应对内部的的EditText 。当用户输入一个字符 onTextChanged 的code运行时,一切正常。因此,如果例如用户类型一,那么 onTextChanged 将开始的运行

I want to react to the user typing inside an EditText so I used the addTextChangedListener method. When the user types a single character the code of onTextChanged is running and everything ok. So if for example the user types "a" then onTextChanged will begin to run.

但是,如果用户键入另一个字符,例如B,onTextChanged不被调用。

But if the user types another character, for example b , onTextChanged is not being called.

(在EditText上的文字应该是AB现在)

(the text in the EditText should be "ab" now)

在code:

et = (EditText)findViewById(R.id.edittextsearch); et.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s){} public void beforeTextChanged(CharSequence s, int start, int count,int after){} public void onTextChanged(CharSequence s, int start, int before,int count) { int i = 0; textlength=et.getText().length(); arr_sort.clear(); for(i=0;i<3;i++) { if(textlength<=your_array_contents[i].length()) { if(et.getText().toString().equalsIgnoreCase((String) your_array_contents[i].subSequence(0, textlength))) { arr_sort.add(your_array_contents[i]); } } } lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this, android.R.layout.simple_list_item_multiple_choice, arr_sort)); } });

帮助是AP preciated!

Help is appreciated!

推荐答案

从code,有什么我能理解,你要过滤的的ListView 。

From your code, What I could understand is, you want to filter the ListView.

而不是自己做的过滤器,你应该使用 listView.setFilterText(字符串)。

Instead of doing filter by yourself you should use listView.setFilterText(String).

像这样的:

添加适配器第一和一次。

add your adapter for first and one time.

lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this, android.R.layout.simple_list_item_multiple_choice, your_array_contents));

然后添加TextWatcher:

and then add TextWatcher:

txtFilter.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { if(s.length()==0){ lv.clearTextFilter(); } } public void beforeTextChanged(CharSequence s, int start, int count, int after){ } public void onTextChanged(CharSequence s, int start, int before, int count) { lv.setTextFilterEnabled(true); lv.setFilterText(s.toString()); } });

更多推荐

安卓:addTextChangedListener工作不正常

本文发布于:2023-11-27 10:02:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正常   工作   addTextChangedListener

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!