【问题标题】:The logic to make a progress bar countdown timer count "up" in Android在Android中使进度条倒数计时器“向上”计数的逻辑
【发布时间】:2015-07-25 19:54:35
【问题描述】:

我似乎不知道应该如何填满进度条。我正在使用倒数计时器来更新进度条上的进度,使其倒计时直到达到零。现在我想扭转进度,让它从零开始并填满,但我该如何实现呢?

public class theCountDownTimer extends CountDownTimer
{
    public theCountDownTimer(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished)
    {
        int progress = (int) (millisUntilFinished/100);
        progressBarCounter.setProgress(progress);
    }

    @Override
    public void onFinish()
    {
        Button nextInstance = (Button) findViewById(R.id.run);
        nextInstance.performClick();
    }
}

【问题讨论】:

  • 添加了处理倒计时的代码
  • @uhlm 如果你解决了你的问题,请分享解决方案。

标签: android


【解决方案1】:

progressBar 默认最大值为100。我猜你希望它在每个滴答声中都取得进展,所以在 theCountDownTimer 中你应该定义:

progressBarCounter.setMax((int) millisInFuture/1000);

这样,如果您计算 80 秒 (= 80,000mS),那么该条的范围将在 0 到 80 之间。
接下来可以使用incrementProgressBy (int diff)方法:

public class theCountDownTimer extends CountDownTimer
{

  public theCountDownTimer(long millisInFuture, long countDownInterval)
  {
    super(millisInFuture, countDownInterval);
    progressBarCounter.setMax((int) millisInFuture/1000);
    progressBarCounter.setProgress(0);  //Reset the progress
  }

  @Override
  public void onTick(long millisUntilFinished)
  {
    progressBarCounter.incrementProgressBy(1);
  }

  @Override
  public void onFinish()
  {
    Button nextInstance = (Button) findViewById(R.id.run);
    nextInstance.performClick();
  }
}

我猜你这里也有错误-

int progress = (int) (millisUntilFinished/100);

您不是要除以1000 而不是100吗?

【讨论】:

  • 我确实相信“100”指的是进度条的内置功能,进度条满时为 100%
  • @uhlm 是的,但有时您想使用其他值。如果您计算 30 秒并在每个刻度上增加 1,那么当计数结束时,您将达到 30/100。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多