How to programmatically trigger the touch event in android?

后端 未结 3 1634
时光取名叫无心
时光取名叫无心 2020-11-30 03:00

I would like to trigger a touch event like this:

First the finger is touch down at the (0,50%) of the screen and slide to the (50%,50%) of the screen, and exit (move

相关标签:
3条回答
  • 2020-11-30 03:22

    And here is the clean version:

    public void TouchView(View view)
    {
        view.DispatchTouchEvent(MotionEvent.Obtain(SystemClock.UptimeMillis(), SystemClock.UptimeMillis(), (int)MotionEventActions.Down, 0, 0, 0));
        view.DispatchTouchEvent(MotionEvent.Obtain(SystemClock.UptimeMillis(), SystemClock.UptimeMillis(), (int)MotionEventActions.Up, 0, 0, 0));
    }
    

    PS: This is a xamarin android solution but you can easily modify it for java

    0 讨论(0)
  • 2020-11-30 03:28
    // Obtain MotionEvent object
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis() + 100;
    float x = 0.0f;
    float y = 0.0f;
    // List of meta states found here:     developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    int metaState = 0;
    MotionEvent motionEvent = MotionEvent.obtain(
        downTime, 
        eventTime, 
        MotionEvent.ACTION_UP, 
        x, 
        y, 
        metaState
    );
    
    // Dispatch touch event to view
    view.dispatchTouchEvent(motionEvent);
    
    0 讨论(0)
  • 2020-11-30 03:32

    To add to the excellent answer by @bstar55 above - if what you want is to create a 'tap' event, e.g, a user tapping on a view, then you need to simulate the user touching the screen and then removing their finger in a short time frame.

    To do this you 'dispatch' an ACTION_DOWN MotionEvent followed after a short time by an ACTION_UP MotionEvent.

    The following example, in Kotlin, is tested and worked reliably:

       //Create amd send a tap event at the current target loctaion to the PhotoView
       //From testing (on an original Google Pixel) a tap event needs an ACTION_DOWN followed shortly afterwards by
       //an ACTION_UP, so send both events
       //First, create amd send the ACTION_DOWN MotionEvent
       var originalDownTime: Long = SystemClock.uptimeMillis()
       var eventTime: Long = SystemClock.uptimeMillis() + 100
       var x = your_X_Value
       var y = your_Y_Value 
       var metaState = 0
       var motionEvent = MotionEvent.obtain(
               originalDownTime,
               eventTime,
               MotionEvent.ACTION_DOWN,
               x,
               y,
               metaState
        )
        var returnVal = yourView.dispatchTouchEvent(motionEvent)
        Log.d(TAG,"rteurnVal: " + returnVal)
    
        //Create amd send the ACTION_UP MotionEvent
        eventTime= SystemClock.uptimeMillis() + 100
        motionEvent = MotionEvent.obtain(
               originalDownTime,
               eventTime,
               MotionEvent.ACTION_UP,
               x,
               y,
               metaState
         )
         returnVal = yourView.dispatchTouchEvent(motionEvent)
         Log.d(TAG,"rteurnVal: " + returnVal)
    
    0 讨论(0)
提交回复
热议问题