onKeyListener not working with soft keyboard (Android)

前端 未结 6 628
太阳男子
太阳男子 2020-11-30 07:07

I am using onKeyListener to get the onKey events. It works fine with the normal keyboard. But it does not work with soft keyboard. I am only able to get onKey events for num

6条回答
  •  感动是毒
    2020-11-30 07:08

    I got around this by putting the listener into it's own method and calling it again after the first time. In the onCreate I call setKeyListenerForEnter();

    Then, here's the method:

    public void setKeyListenerForEnter(){

        final EditText search_entry = (EditText) findViewById(R.id.search_entry);
        search_entry.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    getSearchResults(v);
                    setKeyListenerForEnter();
                  return true;
                }
                return false;
            }
        });
    }
    

    I'm not sure if this is a better solution than handling the IME keyboard itself, but it is a solution.

提交回复
热议问题