0

我正在尝试在按下按钮后更新文本视图,以下是我的代码:

      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();
        }
    }
};
4

3 回答 3

1

试试这个代码:

公共类 XXX {
私人 CountDownMine 倒计时;

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
    }
    }
};

}

于 2013-07-31T15:46:43.097 回答
0
       public void onFinish() {
            // TODO Auto-generated method stub
            repeatCounter = repeatCounter + 1;
            startTimeCounter();
        }

您正在创建new counter每次previous one finishesbutton 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();
        }
    }
};
于 2013-07-29T12:45:26.043 回答
0

CountDownTimer每次startTimeCounter调用时,您都在创建一个新的。而是重新启动上一个:

// Initializing the timer earier
CountdownTimer tripTimeCounter = new CountDownTimer(60 * 1000, 1000) {
    ...
}

定时器启动方式变为:

public void startTimeCounter() {
    tripTimeCounter.cancel();
    tripTimeCounter.start();
}
于 2013-07-29T12:45:50.907 回答