[英]Double click event in android
如何在不使用gesturedetector的情况下在android中实现双击事件?
如果你的意思是双击,你必须使用GestureDetector.OnDoubleTapListener 。
I'm sure all the code there does is determine if the second click was within a certain time of the first click, otherwise treat it as a second click. 我确定那里的所有代码都确定第二次点击是否在第一次点击的特定时间内,否则将其视为第二次点击。 That's how I would do it anyway. 无论如何我就是这样做的。
just use setOnTouchListener to record the first and second click time. 只需使用setOnTouchListener记录第一次和第二次点击时间。 If they are very close, determine it as a double click. 如果它们非常接近,请将其确定为双击。 Like this, 像这样,
public class MyActivity extends Activity {
private final String DEBUG_TAG= "MyActivity";
private long firstClick;
private long lastClick;
private int count; // to count click times
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button mButton= (Button)findViewById(R.id.my_button);
mButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
// if the second happens too late, regard it as first click
if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) {
count = 0;
}
count++;
if (count == 1) {
firstClick = System.currentTimeMillis();
} else if (count == 2) {
lastClick = System.currentTimeMillis();
// if these two clicks is closer than 300 millis second
if (lastClick - firstClick < 300) {
Log.d(DEBUG_TAG,"a double click happened");
}
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
}
}
看这里,这是jar中的库,用于监听触摸手势,实现和工作) https://github.com/NikolayKolomiytsev/zTouch
Look at the source code for GestureDetector
and copy the bits you need (specifically, look at the isConsideredDoubleTap
method) 查看GestureDetector
的源代码并复制您需要的位(具体来说,请查看isConsideredDoubleTap
方法)
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/GestureDetector.java https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/GestureDetector.java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.