Android:addTextChangedListener运行不正常

Android:addTextChangedListener运行不正常,第1张

概述我想对用户在EditText内输入做出反应,所以我使用了addTextChangedListener方法.当用户键入单个字符时,onTextChanged的代码正在运行,一切正常.因此,例如,如果用户键入“a”,则onTextChanged将开始运行.但是如果用户键入另一个字符,例如b,则不会调用onTextChanged.(EditText中的

我想对用户在EditText内输入做出反应,所以我使用了addTextChangedListener方法.
当用户键入单个字符时,onTextChanged的代码正在运行,一切正常.
因此,例如,如果用户键入“a”,则onTextChanged将开始运行.

但是如果用户键入另一个字符,例如b,则不会调用onTextChanged.

(EditText中的文本现在应为“ab”)

代码:

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

感谢帮助!

解决方法:

从您的代码,我可以理解的是,您想要过滤ListVIEw.

您应该使用ListVIEw.setFilterText(String)而不是自己进行过滤.

像这样:

首先添加适配器一次.

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

然后添加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());    }});
总结

以上是内存溢出为你收集整理的Android:addTextChangedListener运行不正常全部内容,希望文章能够帮你解决Android:addTextChangedListener运行不正常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1105425.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存