sei*_*elj 5 android webview ontouchlistener touch-event motionevent
我能够使用 MotionEvent.obtain 在典型的 android 视图(文本框、按钮等)上模拟点击事件,如下所示:
int meta_state = 0;
MotionEvent motionEvent = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,
x,
y,
meta_state
);
motionEvent.recycle();
view_tapped.dispatchTouchEvent(motionEvent);
Run Code Online (Sandbox Code Playgroud)
view_tapped 是对我要将事件发送到的视图的引用。但是,当我在 webview 上调用 dispatchTouchEvent 时,它似乎没有点击里面的任何元素。
我还在 webview 中放置了一个 onTouchListener,然后当视图被手指实际点击时转储与motionevent 相关的所有信息,然后创建一个具有所有相同信息的新motionevent 并将其分派到webview,它没有点击屏幕。onTouchListener 仍然被触发,但屏幕上的元素没有被点击。
我还尝试使用 Javascript 来完成它,通过尝试找出特定坐标处的元素,然后通过注入 javascript 来触发 click 事件,但也没有运气。
关于我做错了什么的任何想法?
有我的解决方案:
private void simulateClick(float x, float y) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent.PointerProperties[] properties = new MotionEvent.PointerProperties[1];
MotionEvent.PointerProperties pp1 = new MotionEvent.PointerProperties();
pp1.id = 0;
pp1.toolType = MotionEvent.TOOL_TYPE_FINGER;
properties[0] = pp1;
MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[1];
MotionEvent.PointerCoords pc1 = new MotionEvent.PointerCoords();
pc1.x = x;
pc1.y = y;
pc1.pressure = 1;
pc1.size = 1;
pointerCoords[0] = pc1;
MotionEvent motionEvent = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, 1, properties,
pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0 );
dispatchTouchEvent(motionEvent);
motionEvent = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_UP, 1, properties,
pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0 );
dispatchTouchEvent(motionEvent);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2236 次 |
| 最近记录: |