2015-06-07 149 views
4

我正在開發我自己的自定義鍵盤。Android手柄「搜索」按鈕按下自定義鍵盤

如果我們的鍵盤用IME_ACTION_SEARCH參數打開,如何處理「搜索」按鈕?

我有以下代碼,但不幸的是在搜索的情況下它不工作。在正常情況下,完成按鈕它運行良好。

 final int options = this.getCurrentInputEditorInfo().imeOptions; 
     final int actionId = options & EditorInfo.IME_MASK_ACTION; 

     switch (actionId) { 
      case EditorInfo.IME_ACTION_SEARCH: 
       ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SEARCH)); 
       break; 
      default: 
       ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)); 
     } 

感謝

回答

7

我找到了解決辦法做到這一點:

endDefaultEditorAction(true); 

它是InputMethodService

的方法完整的代碼是:

case Keyboard.KEYCODE_DONE: 
     final int options = this.getCurrentInputEditorInfo().imeOptions; 
     final int actionId = options & EditorInfo.IME_MASK_ACTION; 

     switch (actionId) { 
      case EditorInfo.IME_ACTION_SEARCH: 
       sendDefaultEditorAction(true); 
       break; 
      case EditorInfo.IME_ACTION_GO: 
       sendDefaultEditorAction(true); 
       break; 
      case EditorInfo.IME_ACTION_SEND: 
       sendDefaultEditorAction(true); 
       break; 
      default: 
       ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)); 
     } 

     break; 
+0

THANK YOU!但爲什麼手動發送事件不起作用?你有什麼想法嗎? – Mohammad