【问题标题】:I want to call onTouchEvent(MotionEvent event) whithin my code with a different MotionEvent object?我想用不同的 MotionEvent 对象在我的代码中调用 onTouchEvent(MotionEvent event) 吗?
【发布时间】:2013-06-17 10:28:47
【问题描述】:

我想在代码中模拟一个触摸事件,换句话说,就是在屏幕上扫一扫。

我想在我的代码中触发 public boolean onTouchEvent(MotionEvent event) 方法,但 MotionEvent 对象应该具有所需的值,以便模拟屏幕上的扫描(例如从左到右的扫描)。

我阅读了this 条目,但我认为这仅在进行 Android 单元测试时才有用,并且我需要从代码的任何部分调用此方法以随时模拟滑动。

我想知道这是否可能。提前致谢。

【问题讨论】:

    标签: android touch-event simulate motionevent


    【解决方案1】:

    How to simulate a touch event in Android?

    MotionEvent motionEvent = MotionEvent.obtain(
        downTime, 
        eventTime, 
        MotionEvent.ACTION_UP, 
        x, 
        y, 
        metaState
    );
    
    // Dispatch touch event to view
    view.dispatchTouchEvent(motionEvent);
    

    【讨论】:

      【解决方案2】:

      感谢迪奥戈·本托。我的代码有些东西:它不是 OnTouchEvent。它是一个 GestureDetector.OnGestureListener。我尝试调用 onSingelTapUp()。

      long time =System.currentTimeMillis();
      MotionEvent motionEvent = MotionEvent.obtain( time, time+1, MotionEvent.ACTION_DOWN, 0, 0, 0);
      view.last.dispatchTouchEvent(motionEvent);
      MotionEvent motionEvent1 = MotionEvent.obtain( time+1, time+2, MotionEvent.ACTION_UP, 0, 0, 0);
      view.dispatchTouchEvent(motionEvent1);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多