0
我有一個計時器,我想在按住按鈕時啓動,並在我放開時重置計時器。這可能與onClick,但我真的不知道如何在這個項目中使用onTouch事件。第一個代碼片段工作,但如何實現觸摸,而不是按照asy的方式點擊?在我的timerproject中實現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;
}
}