[英]how to implement search function in android
嗨,我是 android 新手,我所做的是創建一個帶有字符串的列表並使用編輯文本實現搜索,我的代碼是休耕
((EditText)findViewById(R.id.EditText01)).setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{ String enteredText = ((EditText)findViewById(R.id.EditText01)).getText().toString();
Iterator<String> strIt = strList.iterator();
while(strIt.hasNext()){
String str = strIt.next();
if(str.startsWith(enteredText)){
match.add(str);
}
}
}
當焦點從編輯文本中改變時它起作用,但我需要像快速搜索框一樣工作。當我在編輯文本中輸入一個字母時,將顯示匹配的單詞。我該怎么做。請發布一些代碼。謝謝你提前。
嘗試實現 TextWatcher 接口。
它具有三個您需要覆蓋的方法。
public void afterTextChanged(Editable s) {
Log.v("afterTextChanged","here");
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
Log.v("beforeTextChanged","here");
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
我認為這會完成你的工作。
看看 AutoCompleteTextView。
基本上,您可以創建一個包含字符串的 ArrayAdapter 或 SimpleAdapter。 然后只需將您的 EditText 替換為 AutoCompleteTextView 並自動...您將獲得您描述的預輸入行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.