Android实现模拟点击的一种方法

private void autoClick(View view) {
        long downTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,
                MotionEvent.ACTION_DOWN, view.getWidth() / 2, view.getHeight() / 2, 0);
        downTime += 100;
        final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,
                MotionEvent.ACTION_UP, view.getWidth() / 2, view.getHeight() / 2, 0);
        view.dispatchTouchEvent(downEvent);
        view.dispatchTouchEvent(upEvent);
        downEvent.recycle();
        upEvent.recycle();
}

原理:模拟View的MotionEvent.ACTION_DOWN和MotionEvent.ACTION_UP

你可能感兴趣的:(Android,模拟点击)