我正在尝试更新自按下按钮后经过的时间的文本视图,以下是我的代码:
public void startTimeCounter() {
tripTimeCounter = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onFinish() {
// TODO Auto-generated method stub
repeatCounter = repeatCounter + 1;
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();
}
private static String formatInterval(final long l) {
final long hr = TimeUnit.MILLISECONDS.toHours(l);
final long min = TimeUnit.MILLISECONDS.toMinutes(l
- TimeUnit.HOURS.toMillis(hr));
final long sec = TimeUnit.MILLISECONDS.toSeconds(l
- TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
final long ms = TimeUnit.MILLISECONDS.toMillis(l
- TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min)
- TimeUnit.SECONDS.toMillis(sec));
return String.format("%02d:%02d:%02d", hr, min, sec, 0);
}
计时器工作正常,但是当我第二次按下按钮时,文本视图中的值不会被清除,而是关联一个与前一个计时器并行运行的新时间,我可以看到上一次并反映新时间同时增加。我想知道在第二次调用startTimeCounter()之前我将如何清除先前的状态;
按钮单击代码:
private View.OnClickListener but = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isOnline() == true) {
// PHP();
// network();
startTimeCounter();
} else {
Toast.makeText(getApplicationContext(),
"Not connected to the internet", Toast.LENGTH_LONG)
.show();
}
}
};
答案 0 :(得分:1)
试试这段代码:
公共类XXX
{
private CountDownMine CountDown;
private class CountDownMine extends CountDownTimer
{
public CountDownMine(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish()
{
//The same yours
}
@Override
public void onTick(long millisUntilFinished)
{
//The same yours
}
}
private View.OnClickListener but = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (isOnline() == true)
{
//Code
if(CountDown != null)
CountDown.cancel();
CountDown = new CountDownMine(60 * 1000, 1000);
CountDown.start();
}
else
{
//Code
}
}
};
}
答案 1 :(得分:0)
public void onFinish() {
// TODO Auto-generated method stub
repeatCounter = repeatCounter + 1;
startTimeCounter();
}
您每次new counter
或previous one finishes
都会创建button is clicked
。那就是问题所在。将CountDownTimer
实例保留在startTimeCounter方法之外,并尝试将其传递给startTimeCounter(CountDownTimer tripTimeCounter)
并重新启动计时器onFinish(),
public void onFinish() {
// TODO Auto-generated method stub
repeatCounter = repeatCounter + 1;
this.start();
}
private View.OnClickListener but = new View.OnClickListener() {
tripTimeCounter = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onFinish() {
// TODO Auto-generated method stub
repeatCounter = repeatCounter + 1;
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));
}
};
@Override
public void onClick(View v) {
if (isOnline() == true) {
// PHP();
// network();
tripTimeCounter.start();
} else {
Toast.makeText(getApplicationContext(),
"Not connected to the internet", Toast.LENGTH_LONG)
.show();
}
}
};
答案 2 :(得分:0)
每次调用CountDownTimer
时,您都在创建新的startTimeCounter
。改为重新启动前一个:
// Initializing the timer earier
CountdownTimer tripTimeCounter = new CountDownTimer(60 * 1000, 1000) {
...
}
计时器启动方法变为:
public void startTimeCounter() {
tripTimeCounter.cancel();
tripTimeCounter.start();
}