如何实现onTouch或onClick的onKeyDown方法?

时间:2014-05-20 06:34:53

标签: android

我如何在onTouch上实现onKeyDown方法或在android中实现onClick。当前的方法是硬件键,但我想将其更改为onTouch,所以我该怎么做..

 @Override
public boolean onKeyDown(int keyCode, KeyEvent evt) {
    boolean moved = false;
    switch(keyCode) {
        case KeyEvent.KEYCODE_DPAD_UP:
            moved = maze.move(Maze.UP);
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            moved = maze.move(Maze.DOWN);
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            moved = maze.move(Maze.RIGHT);
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            moved = maze.move(Maze.LEFT);
            break;
        default:
            return super.onKeyDown(keyCode,evt);
    }
        }

1 个答案:

答案 0 :(得分:0)

您可以为Button ...

等视图实现OnTouchListener的onTouch方法
    mButton.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

               switch ( event.getAction() ) {

                case MotionEvent.ACTION_DOWN: break;

                case MotionEvent.ACTION_UP: break;

                }

                return true;
        }
    });