gpt4 book ai didi

安卓:addTextChangedListener 运行不正常

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:25 27 4
gpt4 key购买 nike

我想对用户在 EditText 中键入内容使用react,所以我使用了 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());
}
});

关于安卓:addTextChangedListener 运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7995146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com