private long timeElapsed = 0L; //make this a global variable
//tvTouches could be a TextView or Button or other views
tvTouches.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
timeElapsed = event.getDownTime();
Log.d("setOnTouchListener", "ACTION_DOWN at>>>" + event.getDownTime());
break;
case MotionEvent.ACTION_UP:
timeElapsed = event.getEventTime() - timeElapsed;
Log.d("setOnTouchListener", "ACTION_UP at>>>" + event.getEventTime());
Log.d("setOnTouchListener", "Period of time the view is pressed>>>" + timeElapsed);
Toast.makeText(getApplicationContext(), "Period of time the view is pressed in milliseconds>>>" + timeElapsed, Toast.LENGTH_SHORT).show();
timeElapsed = 0L;
//TODO do something when a certain period of time has passed
break;
default:
break;
}
return true;
}
});
comment accepter les réponses je ne sais pas pouvez-vous me dire –