安卓:addTextChangedListener工作不正常 [英] Android: addTextChangedListener not working well

查看:822
本文介绍了安卓: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工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆