android SEND密钥崩溃

时间:2011-02-17 23:20:13

标签: android keyevent

我需要检查输入键以启动搜索例程。除了某些键盘以外的所有工作似乎都有一个SEND按钮而不是ENTER按钮。按下此按钮时,代码转储。我在下面有一个小样本。有什么想法吗?

tx1.setOnEditorActionListener (new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        System.out.println("Key: " + event.getKeyCode()); //BLOWS UP HERE
        if  (event.getAction() ==  KeyEvent.ACTION_DOWN) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                // ...
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我认为在这种情况下该事件为空。为了检测软键盘上的发送操作,你的onEditorActionListener实际上应该这样做。

onEditorAction(TextView v, int actionId, KeyEvent event){
    if(actionId == EditorInfo.IME_ACTION_SEND){
        send();
    }
    return false;// so the softkeyboard will still close after pressing 'send'
}