我正在尝试在我的Android应用中模拟点击并按住逻辑。我的问题是,当我执行双击时,它会同时执行两个单独的单击和双击。我曾考虑过在单次点击代码中创建一个单独的线程,以等待是否再次发生一次点击,但是我不确定这是否是一种优雅的解决方案。我的代码在下面
if (event.getAction() == MotionEvent.ACTION_UP) {
lastUpX = (int)event.getX();
lastUpY = (int)event.getY();
//Get difference between lastUp and lastDown
int lastDownXDiff = Math.abs(lastDownX-lastUpX);
int lastDownYDiff = Math.abs(lastDownY-lastUpY);
Log.e("Down differences", lastDownXDiff + " " + lastDownYDiff);
if ((System.currentTimeMillis()-lastDownTime < 100) && lastDownXDiff < 50 && lastDownYDiff < 50)
messageQueue.add("CLICK LMB");
messageQueue.add("RELEASE LMB");
}
else if (event.getAction() == MotionEvent.ACTION_DOWN) {
lastDownX = (int)event.getX();
lastDownY = (int)event.getY();
if (lastDownTime == -1) {
lastDownTime = System.currentTimeMillis();
}
//If the user double taps, send a left mouse button click
else {
if (System.currentTimeMillis()-lastDownTime <= 300) {
messageQueue.add("HOLD LMB");
Log.e("Double tap", "Double tap");
}