Bel*_*lgi 3 android android-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));
}
});
Run Code Online (Sandbox Code Playgroud)
感谢帮助!
从你的代码,我能理解的是,你想要过滤ListView.
你应该使用而不是自己过滤listView.setFilterText(String).
像这样:
首先添加适配器一次.
lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this,
android.R.layout.simple_list_item_multiple_choice, your_array_contents));
Run Code Online (Sandbox Code Playgroud)
然后添加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());
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10284 次 |
| 最近记录: |