我想对用户在其中输入的内容做出反应EditText
所以我用了addTextChangedListener
方法。
当用户输入单个字符时,代码为onTextChanged
正在运行,一切正常。
例如,如果用户输入“a”,那么onTextChanged
将开始run.
但是,如果用户键入另一个字符,例如 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());
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)