2015-06-07 111 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