在android中获取键盘事件

时间:2014-07-17 07:34:09

标签: android webview key-events

我正在使用webview,页面具有搜索功能,当用户将视图聚焦到serch安卓键盘打开时,我想处理ime选项。

在Android键盘中我有ime选项作为" GO"按钮。 我已经搜索了很多,并尝试了很多东西,除了窗外的我的电脑,请帮助我,我该怎么办

这是我的代码,我没有得到吐司或记录值。

    @Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub

    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_D:
        Toast.makeText(key.this, "preseed", Toast.LENGTH_LONG).show();
        Log.e("d", "preseed");
        return true;
    case KeyEvent.KEYCODE_F:
        Log.e("f", "preseed");
        Toast.makeText(key.this, "preseed", Toast.LENGTH_LONG).show();
        return true;
    case KeyEvent.KEYCODE_J:
        Log.e("j", "preseed");
        Toast.makeText(key.this, "preseed", Toast.LENGTH_LONG).show();
        return true;
    case KeyEvent.KEYCODE_K:
        Log.e("k", "preseed");
        Toast.makeText(key.this, "preseed", Toast.LENGTH_LONG).show();
        return true;
    default:
        return super.onKeyUp(keyCode, event);
    }

}

1 个答案:

答案 0 :(得分:0)

您可以在搜索视图中添加onEditorActionListener,如下所示:

  editText.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(final TextView view,
                final int actionId, final KeyEvent event) {

            if (event != null && actionId != 7 && actionId != 5) {
                  //manage press of button 7 and 5


            } else {
                activity.hideKeyboard();
                return false;
            }

            if (actionId == EditorInfo.IME_ACTION_DONE
                    || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {

                 //manage press of enter button

            }
            return false;
        }

    });

希望它有所帮助;)