1
我是Android的初學者。現在我正在開發一個簡單的應用程序。我想在應用程序中創建一個計時器。我希望它從10倒數到0(它對用戶是可見的),當它是0時,它應該做平頭。當onTouch事件被調用時它應該開始倒計時。我試過這種方式,但它不起作用。任何人都可以幫忙嗎?Android中的計時器
這裏是我的代碼:
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;