【问题标题】:ending CountDownTimer with a click of a button单击按钮结束 CountDownTimer
【发布时间】:2020-04-25 13:38:51
【问题描述】:

所以我有一个开始按钮 1,当它被单击时,它会从 EditText1 中获取文本,然后调用 CountDownTimer 来调用一个方法,该方法通过其参数从 EditText1 传递文本并每 10 秒在 TextView1 中显示一些内容。但是,如果您决定在 EditText1 中键入其他内容并再次单击开始按钮 1,它将再次调用 CountDownTimer。

在这种情况下,将有 2 个“doThis”CountDownTimer 变量,执行 doThis.cancel() 只会取消第二个“doThis”CountDownTimer 变量。我想知道如何解决这个问题。

[编辑文本1]

[开始按钮 1] [TextView1] [结束按钮 1]
[开始按钮 2] [TextView2] [结束按钮 2]

CountDownTimer doThis;

doThis = new CountDownTimer(10000,10000)
    {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            start();
            TextView text = (TextView)findViewById(R.id.text);
            try {
                Date date = new Date(System.currentTimeMillis());
                text.setText(myMethod(variableFromEditText1));
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();

【问题讨论】:

  • 将 countDownTimer 保存在变量中,然后在 EndButton 上单击调用 countDownTimer.stop ? :) 如果你愿意,也可以重置。
  • cancel ?
  • @ScaryWombat 但是你如何让它知道要取消哪一个?可能同时运行不止一个。
  • new CountDownTimer分配给一个变量
  • @MadaManu 如何将 countDownTimer 保存在变量中?类似 countDownTimer theVariable = new countDownTimer?

标签: java android android-studio button countdowntimer


【解决方案1】:

如果您想同时拥有多个计时器和相关的按钮/文本视图,我建议您将它们保存在一个列表中。 您需要“存储”计时器CountDownTimer cdt = new CountDownTimer [...]。我什至会制作一个包装器来包含开始/结束按钮、文本视图和计时器本身。类似:

class MyTimer {

  Button start;
  Button end;
  TextView text;
  CountDownTimer cdt;

  // add here all required getters/setters and a constructor as needed
// the constructor of this wrapper can take details about the buttons location, text view location, and countdown amounts etc. or set them defaults as deemed necessary



}

然后在 MyTimer 中,您可以将启动/停止/重置的所有逻辑放在一个名称好听的方法中,由按钮注册的事件处理程序调用。

然后列出您想要的任意数量的 MyTimer。 List<MyTimer> myTimers = new ArrayList<>();

此列表将帮助您跟踪您有多少,如果您想添加更多,并允许您配置和管理您想要的一堆计时器。

【讨论】:

  • 如果按钮1被点击两次,2countDownTimer被启动,如果多次点击结束按钮1,它只会取消第二次点击。为什么会这样,我该如何解决。
  • 您需要更新此问题或创建一个新问题 - 这取决于您最终创建这些问题的方式。
  • 我编辑了问题,你能看一下吗?
  • 好的,您在开始事件之外创建新的 CountDownTimer。无需致电.start。然后在按下按钮的句柄中,检查doThis 是否已启动,如果没有,则调用doThis.start()。如果它启动了,那么你什么也不做。与停止类似,在按下停止按钮的句柄中检查 doThis 是否已启动,如果已启动则停止,否则什么也不做。
  • 如果你想要两个计时器,那么你在顶部创建两个 doThis1 和 doThis2... 每个按钮都有开始和停止。
【解决方案2】:

您应该在分配新计时器之前取消计时器 - 这样您就可以跟踪它。

// Declare the timer as private variable to keep track of it
private CountDownTimer doThis;

// Method called once the Edit field is editted
public void onEdit() {
    // Check if timer isn't running already, in case it is, cancel it
    if (doThis != null)
        doThis.cancel();

    // And finally, assign the new timer
    doThis = new CountDownTimer(10000, 10000) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            start();
            TextView text = (TextView) findViewById(R.id.text);
            try {
                Date date = new Date(System.currentTimeMillis());
                text.setText(myMethod(variableFromEditText1));
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();
}

编辑: 显然,这确实允许您一次只运行一个计时器 - 已经有一些建议和可行的方法在 cmets 中运行多个计时器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多