我们如何模拟长按触摸事件?或者我们如何计算屏幕被触摸的时间,所有这些都处于ACTION_DOWN状态?
我终于实现了触摸屏长点击,thx all:
textView.setOnTouchListener(new View.OnTouchListener() {
private static final int MIN_CLICK_DURATION = 1000;
private long startClickTime;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
longClickActive = false;
break;
case MotionEvent.ACTION_DOWN:
if (longClickActive == false) {
longClickActive = true;
startClickTime = Calendar.getInstance().getTimeInMillis();
}
break;
case MotionEvent.ACTION_MOVE:
if (longClickActive == true) {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration >= MIN_CLICK_DURATION) {
Toast.makeText(MainActivity.this, "LONG PRESSED!",Toast.LENGTH_SHORT).show();
longClickActive = false;
}
}
break;
}
return true;
}
});
其中CCD_ 1是类变量。
试试这个。你不需要为此找到破解。
final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
Log.e("", "Longpress detected");
}
});
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
};
为了计算触摸次数,您可以像这里一样获得事件的getPointerCount()
对于长点击,这可能有助于
编辑:希望此链接能帮助您确定触摸持续时间
您必须计算ACTION_DOWN和ACTION_UP事件之间的时间。仅在ACTOIN_DOWN状态下无法计算此时间,因为它是表示LONG TAP事件的TAP的事件序列的START事件