android – 通过触摸事件模拟长按
作者:互联网
我们如何通过触摸事件来模拟长按?或者我们如何在ACTION_DOWN状态下计算触摸屏幕的时间?
解决方法:
我终于实现了一个触摸屏长按,所有:
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;
}
});
其中private boolean longClickActive = false;是一个类变量.
标签:android,event-handling,touch-event,long-press 来源: https://codeday.me/bug/20190712/1441878.html