我正在设计一个递增时钟,它将在按钮单击时执行并更新文本视图的内容,就像时间过去一样。但是每次我按下此按钮时,前一个时钟都不会被清除,一个新实例会启动并与前一个线程并行运行。我无法理解如何停止这种行为。以下是我的代码:
类级变量:
int repeatCounter = 1;
CountDownTimer tripTimeCounter;
==================================================== ===============================
private View.OnClickListener but = new View.OnClickListener() {
public void onClick(View v) {
if (isOnline() == true) {
tripTimeCounter = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onFinish() {
// TODO Auto-generated method stub
repeatCounter = repeatCounter + 1;
this.start();
// startTimeCounter();
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
timedisplay = (TextView) findViewById(R.id.textView3);
timedisplay.setText(formatInterval((repeatCounter * 60)
* 1000 - millisUntilFinished));
}
}.start();
tripTimeCounter.start();
} else {
Toast.makeText(getApplicationContext(),
"Not connected to the internet", Toast.LENGTH_LONG)
.show();
}
}
};