2010-10-21 61 views
2

您好我是新到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); 

    } 
    } 

    } 

它工作時重點是改變形式編輯文本,但我需要像快速搜索box.When工作,當我在編輯文本中輸入一個字母,匹配的單詞將顯示。我可以做這個.pls張貼一些code.Thank你提前。

回答

3

嘗試實現TextWatcher接口。

它有3種方法其中U需要重寫。

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) { 
     } 

我認爲這會做你的工作。

+0

N-Joy很好用回答 – Abhi 2011-11-16 10:51:52

+0

謝謝,eNJOY android好友。 – 2011-11-16 12:04:24

0

看一看AutoCompleteTextView。

基本上你可以創建一個ArrayAdapter或包含您的字符串一個SimpleAdapter。然後,只需使用AutoCompleteTextView替換EditText,然後自動進行......您將獲得您描述的typeahead行爲。

+0

,但我需要顯示所有進來的文本提示view.Is有可能做到這一點 – MaheshBabu 2010-10-21 09:39:27

+0

AutocompleteTextView將創建一個擁有所有符合您的字符串可能的話TextView的列表中的項目。這是你想要的嗎? – 2010-10-26 18:37:49