programmatically execute Touch event in android

前端 未结 4 1808
盖世英雄少女心
盖世英雄少女心 2020-12-09 20:01

Can we execute touch event programmatically in android by some method, such as passing screen co-ordinates(x,y)? Is there any such method? Please guide me.

相关标签:
4条回答
  • 2020-12-09 20:48

    There is a special method for this:

    View.performClick()
    
    0 讨论(0)
  • 2020-12-09 20:49

    You could call

    View.onTouchEvent (MotionEvent)
    

    when you know the destination

    When you want to address some child or grand-child,

    View.dispatchTouchEvent (MotionEvent)
    

    is better

    However, you have to supply an self crafted motion event. The MotionEvent holds the coordinates, too.

    0 讨论(0)
  • 2020-12-09 20:52

    I am not sure if it works, but try this:

    MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, 
                                           x, y, pressure, size, 
                                           metaState, xPrecision, yPrecision, 
                                           deviceId, edgeFlags);
    onTouchEvent(event);
    
    0 讨论(0)
  • 2020-12-09 21:01

    You can try using dispatchTouchEvent and give it MotionEvent instance like below

    dispatchTouchEvent(MotionEvent.obtain(downTime, eventTime, action, x, y, pressure, size, metaState, xPrecision, yPrecision, deviceId, edgeFlags))
    

    you can find more help in this link

    http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent%28android.view.MotionEvent%29

    0 讨论(0)
提交回复
热议问题