【问题标题】:How to Increment a timer when a certain button is clicked?单击某个按钮时如何增加计时器?
【发布时间】:2016-01-07 18:50:50
【问题描述】:

我想知道是否可以在计时器运行时增加秒数、小时数或分钟数。目前我已经在我的应用程序 feedAdapter 中实现了这个答案。我也在 CountDownTimer 中使用了这种增量函数,它确实成功了!但只有当你暂停它然后再次恢复它时。(我们不要讨论它)。所有处理程序和函数都在此链接的答案中!

我在适配器中有 2 个按钮:1:开始是计时器 2:我希望这个按钮增加秒或分钟等。如果有任何帮助,将不胜感激!

How to handle multiple countdown timers in ListView?

public void incrementExpirationTime () {

       int defaultIncrementValue = 10000; //lets say ten seconds (this can also be a long data)
       long productExpiryTime = getProductExpiryTime();
       productExpiryTime+=defaultIncrementValue;
}

我也将此private long productExpiryTime; 设为实例,但这也无济于事。

【问题讨论】:

  • 好的,有 2 个按钮和一个 textview?因此,例如希望文本视图在 12 秒时点击更改为 22 并继续计数 23、24、25、26。如果是这样,那应该很容易
  • @Jasz 不!它必须从26、25、24、23等开始继续。请参考链接!
  • @Jasz 先生,我正在使用button.setText(String);

标签: android countdowntimer


【解决方案1】:

您可以修改它以使其变得更好。 我就是这样做的

private final int INCREMENT = 10;
private final int ONE_SECOND = 1;
private boolean running = false;
private int time = 100;

public void initialize() {
    final Button start = findViewById(R.id.start);
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            running = !running;
            if (running)
                updateButton(start);
        }
    });

    final Button increment = findViewById(R.id.increment);
    increment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (running)
                time += INCREMENT;
        }
    });
}

private void updateButton(final Button start) {
    start.postDelayed(new Runnable() {
        @Override
        public void run() {
            time--;
            start.setText(String.valueOf(time));
            if (time == 0) {
                Toast.makeText(getApplicationContext(), "time is up!", Toast.LENGTH_SHORT).show();
                running = false;
            } else {
                if (running) {
                    updateButton(start);
                }
            }
        }
    }, ONE_SECOND);
}

【讨论】:

  • 我从 100 开始,每次你做时间 - 它显示它是 100 然后递减它。它相当于时间 = 时间 - 1;一秒钟我需要制作和编辑
猜你喜欢
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 2011-06-09
  • 2015-12-27
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
相关资源
最近更新 更多