null keyevent and actionid = 0 in onEditorAction() (Jelly Bean / Nexus 7)

后端 未结 6 759
醉梦人生
醉梦人生 2020-12-03 10:18

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 en

6条回答
  •  鱼传尺愫
    2020-12-03 10:43

    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();
        }
    }
    

提交回复
热议问题