Simulate Long press by Touch events

前端 未结 4 1539
执念已碎
执念已碎 2021-02-06 18:08

How can we simulate long press by touch event? or how can we calculate the time that screen is touched, all in ACTION_DOWN state?

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 19:11

    Try this. You don't need to find hack for this.

    final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
     public void onLongPress(MotionEvent e) {
      Log.e("", "Longpress detected");
     }
    });
    
    public boolean onTouchEvent(MotionEvent event) {
     if (gestureDetector.onTouchEvent(event)) {
      return true;
     }
     switch (event.getAction()) {
      case MotionEvent.ACTION_UP:
       break;
      case MotionEvent.ACTION_DOWN:
       break;
      case MotionEvent.ACTION_MOVE:
       break;
     }
     return true;
    }
    };
    

提交回复
热议问题