如何正确覆盖Android中的dispatchKeyEvent

时间:2014-03-18 04:18:34

标签: android keyevent

我得到了

D/InputEventConsistencyVerifier( 2144): KeyEvent: ACTION_UP but key was not down.`
D/InputEventConsistencyVerifier( 2144): in com.android.internal.policy.impl.PhoneWindow$DecorView@41689658
当我尝试在我的活动中执行此操作时,

警告全部:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if ((/*some boolean checks*/) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
            /*some custom functions*/
            return true;
        }
    }
    return super.dispatchKeyEvent(event);
}

在活动级别覆盖dispatchKeyEvent的正确方法是什么?这些警告好吗?我应该在onKeyUponKeyDown我的观点中进行相应的更改吗?

1 个答案:

答案 0 :(得分:6)

试试这个

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
        if (event.getAction() == KeyEvent.ACTION_UP){

         enter();

            return true;
    }}
    return super.dispatchKeyEvent(event);
};