Android检测到长按按钮

我使用遥控器访问设备 .

在我的情况下,当我按下keydown上的任何按钮和键盘事件被连续调用 .

我认为onkey up事件只应在释放键时调用,但在按下按钮时调用它 .

Is there any way to solve this problem or alternative solution to track the onkey long press ?

回答(1)

2 years ago

long lastDown;
long keyPressedDuration ;

button.setOnTouchListener(new OnTouchListener() {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
           lastDown = System.currentTimeMillis();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
           keyPressedDuration = System.currentTimeMillis() - lastDown;
        }
     }
  };