关于Android按键处理

在dispatchKeyEvent(KeyEvent event)中按键的按下抬起都会进行操作,在处理完一个按键操作时无论return true 还是 false ,onKeyDown(int keyCode, KeyEvent event)和onKeyUp(int keyCode, KeyEvent event)都不会再进行按键处理:

1、第一种情况及log信息:

public void DEBUG(String str) {
        Log.d(TAG, str);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG(" onKeyDown " + keyCode );
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG("  onKeyLongPress " + keyCode );
        return super.onKeyLongPress(keyCode, event);
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG("  dispatchKeyEvent 1 key = " + event.getKeyCode() + " event =  " + event.getAction());
        if(event.getAction()==KeyEvent.ACTION_DOWN){
            DEBUG("  dispatchKeyEvent 2 ACTION_DOWN" + event.getKeyCode() );
            return super.dispatchKeyEvent(event);
        }
        if(event.getAction()==KeyEvent.ACTION_UP){
            DEBUG("  dispatchKeyEvent 3 ACTION_UP" + event.getKeyCode() );
            return super.dispatchKeyEvent(event);
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG(" onKeyUp " +  keyCode);
        return super.onKeyUp(keyCode, event);
    }

log信息:
D/ testMainActivity (  853):   dispatchKeyEvent 1 key = 23 event =  0
D/ testMainActivity (  853):   dispatchKeyEvent 2 ACTION_DOWN23
D/ testMainActivity (  853):  onKeyDown 23
D/ testMainActivity (  853):   dispatchKeyEvent 1 key = 23 event =  1
D/ testMainActivity (  853):   dispatchKeyEvent 3 ACTION_UP23
D/ testMainActivity (  853):  onKeyUp 23

2、第二种情况及log信息:

public void DEBUG(String str) {
        Log.d(TAG, str);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG(" onKeyDown " + keyCode );
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG("  onKeyLongPress " + keyCode );
        return super.onKeyLongPress(keyCode, event);
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG("  dispatchKeyEvent 1 key = " + event.getKeyCode() + " event =  " + event.getAction());
        if(event.getAction()==KeyEvent.ACTION_DOWN){
            DEBUG("  dispatchKeyEvent 2 ACTION_DOWN" + event.getKeyCode() );
            return true;
        }
        if(event.getAction()==KeyEvent.ACTION_UP){
            DEBUG("  dispatchKeyEvent 3 ACTION_UP" + event.getKeyCode() );
            return false;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG(" onKeyUp " +  keyCode);
        return super.onKeyUp(keyCode, event);
    }













log信息:
D/ testMainActivity (  711):   dispatchKeyEvent 1 key = 23 event =  0
D/ testMainActivity (  711):   dispatchKeyEvent 2 ACTION_DOWN23
D/ testMainActivity (  711):   dispatchKeyEvent 1 key = 23 event =  1
D/ testMainActivity (  711):   dispatchKeyEvent 3 ACTION_UP23

综上所述:Android系统按键操作最先是在dispatchKeyEvent中进行处理的,然后才是onKeyDown(int keyCode, KeyEvent event)和onKeyUp(int keyCode, KeyEvent event)。


你可能感兴趣的:(android,String,UP,action)