这应该可以工作,在这里找到:如何在 Android 中模拟触摸事件?:
// 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);
有关获取 MotionEvent 对象的更多信息,这是一个很好的答案:Android:如何创建 MotionEvent?
编辑:并获取视图的位置,对于x
和y
坐标,使用:
int[] coords = new int[2];
myView.getLocationOnScreen(coords);
int x = coords[0];
int y = coords[1];