如果启动了新计时器,则重置计时器

时间:2012-07-20 16:47:21

标签: android reset countdowntimer

看到该应用有两个按钮。一个启动30秒计时器,另一个启动60秒计时器。启动计时器没有问题。问题是,比如我点击60秒按钮然后立即点击30秒按钮,文本视图在从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));
    }
}
}

2 个答案:

答案 0 :(得分: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

}

答案 1 :(得分:0)

@Marcus虽然你的答案不正确,但实际上它是。

我只需创建两个单独的对象,如果另一个被激活,则取消其中一个。

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

这是解决方案。