音量UP长按无法启动android中的新活动

时间:2013-12-30 18:37:25

标签: android

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();

        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_UP) {
                //navigate up
            }

            return true;

        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                //navigate down
               }
            return true;
        default:
            return super.dispatchKeyEvent(event);
       }


    }

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
       startActivity(new Intent(getApplicationContext(), fish.class));
       return true;
    }

    return super.onKeyLongPress(keyCode, event);
}

我在类中有几个按钮,我想使用音量增大/减小键进行导航。并且想要在长按键的同时按下按钮。 通过按钮导航工作完全正常,但长按单击该类中的按钮不起作用。

1 个答案:

答案 0 :(得分:1)

使用

public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
       startActivity(new Intent(getApplicationContext(), FISH.class));
       return true;
    }

    return super.onKeyLongPress(keyCode, event);
}

如果您有FISH类扩展活动。