onEditorAction() 中的 null keyevent 和 actionid = 0 (Jelly

时间:2023-03-28
本文介绍了onEditorAction() 中的 null keyevent 和 actionid = 0 (Jelly Bean/Nexus 7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编辑文本,它在我的应用程序中用作搜索框.在我的 Nexus 7 上的 Jelly Bean 中,当我在我正在收听的文本框中输入一些内容并点击输入 KeyEvent = null 和 ActionId = 0 传递给 onEditorAction() 方法.有人遇到过这种情况么?我认为这可能是一个错误.

I have an edit text which functions as a search box in my application. In Jelly Bean on my Nexus 7 when I type something into the text box which I am listening on and hit enter the KeyEvent = null and ActionId = 0 passed into the onEditorAction() method. Has anyone else encountered this? I'm thinking it might be a bug.

在下面的第二个 if 语句中,我得到一个空指针,因为 actionId = 0 和 KeyEvent = null;

In the second if statement below I get a null pointer because the actionId = 0 and KeyEvent = null;

// Search field logic.
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    Log.d(TAG, "onEditorAction");
    if (event != null && event.getAction() != KeyEvent.ACTION_DOWN)
        return false;
    if (actionId == EditorInfo.IME_ACTION_SEARCH
            || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
              .....Do some stuff();
     }
}

推荐答案

最终添加了对 KeyEvent 的空检查.感谢 commonsware 指出这发生在 3.0+ 上.看起来更像是一种解决方法而不是解决方案,但它确实有效.

Ended up adding in a null check for KeyEvent. Thanks to commonsware for pointing out this happens on 3.0+. Seems more like a workaround then a solution, but it works.

// Search field logic.
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    Log.d(TAG, "onEditorAction");
    if (event != null && event.getAction() != KeyEvent.ACTION_DOWN) {
        return false;
    } else if (actionId == EditorInfo.IME_ACTION_SEARCH
        || event == null
        || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
              .....Do some stuff();
    }
}

这篇关于onEditorAction() 中的 null keyevent 和 actionid = 0 (Jelly Bean/Nexus 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Android在不丢失光标的情况下从EditText隐藏软键盘 下一篇:与 EditText 字段重叠的软键盘

相关文章

最新文章