如何知道用户是否在edittext android中按空格

时间:2014-09-24 08:03:28

标签: android-edittext onkeydown textwatcher

以下是我用来获取键事件的方法,这些方法在模拟器上运行正常,但似乎无法在设备上运行。

以下是我用来获取键事件的方法,这些方法在模拟器上运行正常,但似乎无法在设备上运行。

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if (keyCode == KeyEvent.KEYCODE_SPACE) {
        Log.e("onkeydown", "onkeydown");
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // TODO Auto-generated method stub
    if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
        Log.e("dispatch", "dispatch");
    }
    return super.dispatchKeyEvent(event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
        Log.e("onkeyup", "onkeyup");
    }
    return super.onKeyUp(keyCode, event);
}

1 个答案:

答案 0 :(得分:0)

试试这个..

editText.setOnKeyListener(new OnKeyListener() {           
        @Override
        public boolean onKey(View v, int key, KeyEvent event) {
            if (event.getAction()==KeyEvent.ACTION_DOWN && key == KeyEvent.KEYCODE_ENTER) {
                //write code when key press
                return true;
            }
            return false;
        }
    });