Android下模拟点击事件

要实现在WebView上模拟点击效果,效果一直不好,函数是搜索得到的,有几点需要注意:

1、按下和松开之间的时间要相同

2、用dispatchTouchEvent分发事件

3、调用recycle()回收事件

        private void setSimulateClick(View view, float x, float y) {
            long downTime = SystemClock.uptimeMillis();
            final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,
                MotionEvent.ACTION_DOWN, x, y, 0);
            //downTime += 2500;
            final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,
                MotionEvent.ACTION_UP, x, y, 0);
//          view.onTouchEvent(downEvent);
//          view.onTouchEvent(upEvent);
            view.dispatchTouchEvent(downEvent);
            view.dispatchTouchEvent(upEvent);
            downEvent.recycle();
            upEvent.recycle();
        }  

你可能感兴趣的:(Android下模拟点击事件)