2010-10-15 158 views
1

嗨,我是android新手,我做的是用編輯文本實現搜索,我將 編寫爲休閒文件。如何實現帶編輯文本的快速搜索功能

((EditText)findViewById(R.id.EditText01)).setOnKeyListener(new OnKeyListener() { 
       public boolean onKey(View v, int keyCode, KeyEvent event) { 
         if(event.getAction() == KeyEvent.ACTION_UP){ 

       String enteredText = ((EditText)findViewById(R.id.EditText01)).getText().toString(); 

       if(enteredText.length() > 0) { 
       String str1 =""+ enteredText.charAt(0); 

       str1 = str1.toUpperCase(); 
       String str2 =""+ enteredText.substring(1); 
       str2 = str2.toLowerCase(); 
       enteredText = str1 + str2; 

       } 
       id=0; 
       Iterator<String> strIt = strList.iterator(); 
       Log.v("", "Total items in the list****"+strList); 
       ((LinearLayout)findViewById(R.id.LinearLayout02)).removeAllViewsInLayout(); 
       if(strIt.next().startsWith(enteredText)) 
       { 
        Log.v("", "hi u r enterd letter is there in list"); 
       } 
       else 
       { 
        ((LinearLayout)findViewById(R.id.LinearLayout02)).removeAllViewsInLayout(); 
        Toast.makeText(MyShoppingList.this, "There is no item In the List Start with Letter "+enteredText+" Click Add Item to Add New Item" ,06).show(); 
        Log.v("", " u enterd letter is no there in list"+enteredText); 
        additem(); 

       } 

       count = 0; 
       while(strIt.hasNext()){ 
        String str = strIt.next(); 

        if(str.startsWith(enteredText)){ 

         //count++; 
        matchingLetters(str); 

       } 


       } 

       } 

         return false;  } 

      }); 

它工作正常,但是當在鍵盤主動一會兒,然後最小化鍵盤代碼只運行在active.it doee設備的鍵盤實現我收到的問題。有什麼問題我不明白請幫助我。舉一些代碼或鏈接。謝謝你提前。

回答

1

請注意: onKey()會返回一個布爾值來指示您是否消耗了該事件,並且不應該繼續執行該事件。也就是說,返回true表示你已經處理了事件,它應該停在這裏;如果你還沒有處理它並且/或者這個事件應該繼續給任何其他的on-key偵聽器,則返回false。 http://developer.android.com/reference/android/view/View.OnKeyListener.html#onKey(android.view.View,int,android.view.KeyEvent)

所以我認爲你必須返回true &然後將結果回發給StackOverflow。