2010-10-15 112 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。