我想在用户打字,所以我使用了 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工作不正常
发布评论