Android – simulate a long press by touching an event
•
Android
How do we simulate a long press by touching an event? Or how do we in action_ Calculate the time to touch the screen in the down state?
resolvent:
I finally realized a touch screen long press, 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;
}
});
Where private Boolean longclickactive = false; Is a class variable
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码