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

后端 未结 6 757
醉梦人生
醉梦人生 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 11:08

    It might be worth noting, that you can get more than one event for the click on Enter (depending on the android version). One for the KeyDown (KeyEvent.ACTION_DOWN), one for the KeyUp (KeyEvent.ACTION_UP). When I forgot to check that I accidentally started two server calls for the same action.

    searchBox.setOnEditorActionListener(new OnEditorActionListener() {
    // enter key in search box triggers search
    @Override
    public boolean onEditorAction(TextView v, int actionId,
            KeyEvent event) {
        if ((event != null && event.getAction() == KeyEvent.ACTION_UP) || event==null) {
            onSearchButtonClicked();
        }
        return true;
    }
    });
    

提交回复
热议问题