长按WebView系统默认进入文字选择默认,但如果想实现先长按菜单显示菜单同能菜单选项来判断是否进行的进入自由复制文本文字选择的操作就不知道该怎么办了。
长按WebView显示弹出菜单,可怎么才在能实现点击菜单选择进入自由复制文本模式呢?在网上翻了个遍,都不怎么如意,纠结了几日,下午偷懒睡了一觉突然就想到了Instrumentation模拟长按操作触发WebView默认的显示复制菜单
先实现模拟长按操作实现类
/**
* 模拟点击事件和长按事件
*
*/
public class TouchEventRunnable implements Runnable {
private int x;
private int y;
private boolean isLongPress;
public TouchEventRunnable(int x, int y) {
this.x = x;
this.y = y;
}
public TouchEventRunnable(int x, int y, boolean isLongPress) {
this.x = x;
this.y = y;
this.isLongPress = isLongPress;
}
@Override
public void run() {
if(isLongPress){
longClickOnScreen(x,y);
}else{
onClick();
}
}
private void longClickOnScreen(int x, int y) {
final Instrumentation inst = new Instrumentation();
try {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
final MotionEvent eventDown = MotionEvent.obtain(downTime,
eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
eventDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
final MotionEvent eventMove = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_MOVE, x, y+1, 0);
eventMove.setSource(InputDevice.SOURCE_TOUCHSCREEN);
final MotionEvent eventUp = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_UP, x, y, 0);
eventUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
inst.sendPointerSync(eventDown);
inst.sendPointerSync(eventMove);
try {
Thread.sleep(650);
} catch (InterruptedException e) {
e.printStackTrace();
}
inst.sendPointerSync(eventUp);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
private void onClick(){
Instrumentation inst = new Instrumentation();
inst.sendPointerSync(MotionE