【问题标题】:Reset timer if a new timer is started如果启动了新计时器,则重置计时器
【发布时间】:2012-07-20 16:47:20
【问题描述】:

看到该应用有两个按钮。一个启动三十秒计时器,另一个启动六十秒计时器。启动计时器没有问题。问题是,假设我点击了 60 秒按钮,然后立即点击 30 秒按钮,textview 在从 60 到 0 和 30 到 0 倒计时之间切换。它变成 59 28 57 26 等等..我想要什么要知道的是,假设我先点击 60 秒,然后点击 30 秒按钮,我希望取消 60 秒倒计时并开始 30 秒倒计时。

这是我的代码。我只发布相关的部分。

[编辑]我现在已经发布了整个代码。

package com.android.tapme;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TapMe extends Activity {

private int countValue = 0, psuedoCountValue = 0;
private TextView textView1;
private TextView textView2;
Button tapButton;
Button sixty_seconds;
Button thirty_seconds;
private boolean thirtyon=false, sixtyon=false;
boolean timeUp=false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tap_me);
    tapButton= (Button) findViewById(R.id.tapButton);
    textView1 = (TextView) findViewById(R.id.textView1);
    textView1.setTextSize(40);
    textView2 = (TextView) findViewById(R.id.textView2);
    textView2.setTextSize(20);
    thirty_seconds = (Button) findViewById(R.id.thirty_seconds);
    sixty_seconds= (Button) findViewById(R.id.sixty_seconds);
    sixty_seconds.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  timeUp=false;
                  countValue=0;
                  sixtyon=true;
                  checkTapValue();
                  MyCount myCount=new MyCount(60000,1000);
                  if(thirtyon==true)
                  {
                      myCount.cancel();
                  }
                  myCount=new MyCount(60000,1000);
                  thirtyon=false;
                  myCount.start();
            }
            });
    thirty_seconds.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                timeUp=false;
                countValue=0;
                thirtyon=true;
                checkTapValue();
                MyCount myCount=new MyCount(30000,1000);
                if(sixtyon==true)
                {
                    myCount.cancel();
                }
                myCount=new MyCount(30000,1000);
                sixtyon=false;
                myCount.start();
      }
    });
}
private void checkTapValue() {
    tapButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if(timeUp==false)
            {
            countValue++;
            textView1.setText(Integer.toString(countValue));
            }
            else if(timeUp==true)
            {
            psuedoCountValue=countValue;
            textView1.setText(Integer.toString(psuedoCountValue));
            }
        }
    });

}
public void disable_Button()
{       
    timeUp=true;
    psuedoCountValue=countValue;
}

class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }
    @Override
    public void onFinish() {
        disable_Button();
        textView2.setText("Time's up!");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        System.out.println(millisUntilFinished);
        textView2.setText("" + (int) (millisUntilFinished / 1000));
    }
}
}

【问题讨论】:

    标签: android reset countdowntimer


    【解决方案1】:

    您没有保留对当前 myCount 的引用。你现在正在做的是创建一个新的(甚至还没有开始),然后取消它。

    它应该看起来像这样:

    public class TapMe extends Activity {
    
        // all other fields
    
        private MyCount currentCount;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            // code...
    
            sixty_seconds.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    timeUp=false;
                    countValue=0;
                    sixtyon=true;
                    checkTapValue();
                    if (currentCount != null) {
                        currentCount.cancel();
                    }
                    currentCount=new MyCount(60000,1000);
                    thirtyon=false;
                    currentCount.start();
                }
            );
    
            // code...
    
        }    
    
        // the rest of the code
    
    }
    

    【讨论】:

    • 我没有输入三十秒计数器的更新代码,但应该以同样的方式进行更改。
    【解决方案2】:

    @Marcus 虽然你的答案不是正确的,但本质上是正确的。

    我只需要创建两个单独的对象并在另一个被激活时取消其中一个。

                          if(thirtyCount!=null)
                      {
                          thirtyCount.cancel();
                      }
                      sixtyCount.start();
    

    这就是解决方案。

    【讨论】:

    • 很确定我的解决方案是正确的 - 因为您在任何时候只运行一个计数器,只存储一个对象就足够了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多