android SEND键崩溃

发布于 2024-10-18 03:02:21 字数 521 浏览 5 评论 0原文

我需要检查回车键才能启动搜索例程。除了某些键盘之外,所有作品似乎都有一个“发送”按钮而不是“输入”按钮。当按下此键时,代码会转储。我下面有一个小样本。有什么想法吗?

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) {
                // ...
            }
        }
    }
}

I have a need to check for the enter key to start a search routine. All works except some keyboards seem to have a SEND button instead of the ENTER button. When this is pressed the code dumps. I have a small sample below. Any ideas?

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) {
                // ...
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2024-10-25 03:02:21

我相信在这种情况下该事件为空。为了检测软键盘上的发送操作,您的 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'
}

I believe that event is null in this case. for detecting the send action on the softkeyboard your onEditorActionListener should actually just do this.

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'
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文