我是android的初学者。现在我正在开发一个简单的应用程序。我想在应用程序中创建一个计时器。我希望它从 10 倒计时到 0(它将对用户可见),当它为 0 时,它应该做 smth。当 onTouch 事件被调用时,它应该开始倒计时。我试过这种方式,但它不起作用。有人可以帮忙吗?
这是我的代码:
final MyCounter timer = new MyCounter(10000,1000);
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
System.out.println("Timer Completed.");
time.setText("Timer Completed.");
}
@Override
public void onTick(long millisUntilFinished) {
time.setText((millisUntilFinished/1000)+"");
System.out.println("Timer : " + (millisUntilFinished/1000));
}
}
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
timer.start();
}
return false;