如何在按钮被点击时重置计时器?

huangapple go评论64阅读模式
英文:

How can i reset the timer if my button is clicked?

问题

我有一个TextView,并且我制作了一个计时器,从45秒倒数到0。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forgot_password);
    TextView textView = (TextView) findViewById(R.id.textView6);
    t1 = (TextView)findViewById(R.id.t1);
    b1 = (Button) findViewById(R.id.button1);

    new CountDownTimer(45000, 1000) {

        public void onTick(long millisUntilFinished) {
            t1.setText("00:" + millisUntilFinished / 1000);
            if(millisUntilFinished <= 10000){
                t1.setText("00:0" + millisUntilFinished / 1000);
            }
        }

        public void onFinish() {
            t1.setText("Tempo acabado");
            b1.setEnabled(true);
        }
    }.start();
}

我希望在时间到期后,给用户一个选项,让用户点击b1按钮重新启动计时器。

英文:

I have a textview and i made a timer to decrease from 45 seconds to 0

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forgot_password);
   TextView textView = (TextView) findViewById(R.id.textView6);
    t1 = (TextView)findViewById(R.id.t1);
    b1 = (Button) findViewById(R.id.button1);

    new CountDownTimer(45000, 1000) {

        public void onTick(long millisUntilFinished) {
            t1.setText(&quot;00:&quot; + millisUntilFinished / 1000);
            if(millisUntilFinished&lt;=10000){
                t1.setText(&quot;00:0&quot; + millisUntilFinished / 1000);
            }
        }

        public void onFinish() {
            t1.setText(&quot;Tempo acabado&quot;);
            b1.setEnabled(true);

        }
    }.start();

I want after the time has expired give the option for the user to click the b1 button and restart the timer

答案1

得分: 0

你可以创建CountDownTimer的实例,然后在需要重新启动时,可以对该实例执行cancelstart操作。

CountDownTimer countDownTimer = new CountDownTimer(45000, 1000) {
    //......................
}
//首次启动
countDownTimer.start();

//当按钮被点击时重新启动
b1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        countDownTimer.cancel(); 
        countDownTimer.start();
    }
});
英文:

You can create instance of CountDownTimer, then when you need to restart you can perform cancel and start on that instance.

CountDownTimer countDownTimer = new CountDownTimer(45000, 1000) {
    //......................
}
//Start at first
countDownTimer.start();

//Restart when button is pressed
b1.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
        countDownTimer.cancel(); 
        countDownTimer.start();
       }
    });

答案2

得分: 0

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
TextView textView = (TextView) findViewById(R.id.textView6);
t1 = (TextView) findViewById(R.id.t1);
b1 = (Button) findViewById(R.id.button1);

b1.setOnClickListener(view -> restartTimer());

restartTimer();

}

private void restartTimer() {
b1.setEnabled(false);
new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
t1.setText("00:" + millisUntilFinished / 1000);
if (millisUntilFinished <= 10000) {
t1.setText("00:0" + millisUntilFinished / 1000);
}
}
public void onFinish() {
t1.setText("Tempo acabado");
b1.setEnabled(true);
}
}.start();
}

英文:

Is this what you want?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forgot_password);
    TextView textView = (TextView) findViewById(R.id.textView6);
    t1 = (TextView) findViewById(R.id.t1);
    b1 = (Button) findViewById(R.id.button1);

    b1.setOnClickListener(view -&gt; restartTimer());

    restartTimer();
}

private void restartTimer() {
    b1.setEnabled(false);
    new CountDownTimer(45000, 1000) {
        public void onTick(long millisUntilFinished) {
            t1.setText(&quot;00:&quot; + millisUntilFinished / 1000);
            if (millisUntilFinished &lt;= 10000) {
                t1.setText(&quot;00:0&quot; + millisUntilFinished / 1000);
            }
        }
        public void onFinish() {
            t1.setText(&quot;Tempo acabado&quot;);
            b1.setEnabled(true);
        }
    }.start();
}

huangapple
  • 本文由 发表于 2020年10月1日 04:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64145526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定