最近在用WebView实现EditText时,不希望在双击或长按时出现上下文工具栏,因为这样子不可控,在设置不可点击时依旧能够剪切粘贴,所以就在网上搜搜搜。
结果是,大多数都是
webView.setLongClickable(false);
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
只是解决了长按的问题,没有关于双击的。
所以,还是自己写吧!
richView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
if (isFastClick()){
return true;
}
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
});
private static Map<String, Long> records = new HashMap<>();
private static final int MIN_DELAY_TIME = 1000; // 两次点击间隔不能少于1000ms
public static boolean isFastClick() {
if (records.size() > MIN_DELAY_TIME) {
records.clear();
}
//本方法被调用的文件名和行号作为标记
StackTraceElement ste = new Throwable().getStackTrace()[1];
String key = ste.getFileName() + ste.getLineNumber();
Long lastClickTime = records.get(key);
long thisClickTime = System.currentTimeMillis();
records.put(key, thisClickTime);
if (lastClickTime == null) {
lastClickTime = 0L;
}
long timeDuration = thisClickTime - lastClickTime;
return 0 < timeDuration && timeDuration < 500;
}
自己动手,丰衣足食,目前试着没问题,欢迎指教!