事件(四)

Android常用的物理按键

  • KEYCODE_POWER 电源键
  • KEYCODE_MENU 菜单键
  • KEYCODE_BACK 后退键
  • KEYCODE_HOME Home键
  • KEYCODE_VOLUME_UP 音量键+
  • KEYCODE_VOLUME_DOWN 音量键-
  • KEYCODE_DPAD_CENTER/KEYCODE_ENTER 确认键
  • KEYCODE_DPAD_UP 向上
  • KEYCODE_DPAD_DOWN 向下
  • KEYCODE_DPAD_LEFT 向左
  • KEYCODE_DPAD_RIGHT 向右
  • KEYCODE_0 ~ KEYCODE_9 数字键
  • KEYCODE_A ~ KEYCODE_Z 字母键

常用回调监听

onKeyUp(int keyCode, KeyEvent event);
onKeyLongPress(int keyCode, KeyEvent event);
dispatchKeyEvent(KeyEvent event) ;
onKeyDown(int keyCode, KeyEvent event);

如下:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
           
            break;
        case KeyEvent.KEYCODE_ENTER:
            
            break;
        case KeyEvent.KEYCODE_BACK:
           
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
          
            return false;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            
            return false;
        case KeyEvent.KEYCODE_0:
            
            break;
        case KeyEvent.KEYCODE_1:
           
            break;
        case KeyEvent.KEYCODE_2:
            
            break;
        case KeyEvent.KEYCODE_3:
            
            break;
        case KeyEvent.KEYCODE_4:
            
            break;
        case KeyEvent.KEYCODE_5:
            
            break;
        case KeyEvent.KEYCODE_6:
            
            break;
        case KeyEvent.KEYCODE_7:
            
            break;
        case KeyEvent.KEYCODE_8:
            
            break;
        case KeyEvent.KEYCODE_9:
            
            break;
    }
    return super.onKeyDown(keyCode, event);
}

该方法的返回值为一个boolean类型的变量,当返回true时,表示已经处理完了这个事件,不再继续进行处理;而当返回false时,表示并没有处理完该事件,将继续对其进行处理。

你可能感兴趣的:(事件(四))