我有一个计时器,我想在按住按钮时启动,并在我松开时重置计时器。onClick 是可能的,但我真的不知道如何在这个项目中使用 onTouch 事件。第一个代码片段有效,但如何实现触摸而不是点击?
@Override
public void onClick(View v)
{
if (!timerHasStarted)
{
countDownTimer.start();
timerHasStarted = true;
startB.setText("Start");
}
else
{
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESET");
}
}
public boolean onTouch(View v, MotionEvent event) {
final float mouseSensitivity = 0.5f;
if(event.getAction()==MotionEvent.ACTION_DOWN){
countDownTimer.start();
timerHasStarted = true;
} else if(event.getAction()==MotionEvent.ACTION_UP){
countDownTimer.cancel();
timerHasStarted = false;
}
return timerHasStarted; } }
public void onTouch(View v, MotionEvent event) {
// final float mouseSensitivity = 0.5f;
if(event.getAction()==MotionEvent.ACTION_DOWN){
countDownTimer.start();
timerHasStarted = true;
} else if(event.getAction()==MotionEvent.ACTION_UP){
countDownTimer.cancel();
timerHasStarted = false;
}
}