1

我正在使用 Android Studio。我正在创建一个具有倒数计时器和按钮的应用程序。当我运行应用程序时,计时器会自动运行,如果我想单击按钮,则会有一个 TextView 来计算我按下按钮的次数。我希望该计时器在我按下按钮时启动,倒计时继续运行并在时间“0:000”停止,因此它甚至停止点击计数。你能帮助我吗?(如果它有助于我输入代码)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtCount = (TextView)findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount = (Button)findViewById(R.id.button1);
    final TextView textViewTimer = (TextView)findViewById(R.id.textView2);
    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            count++;
            txtCount.setText(String.valueOf(count));
        }

    });
    new CountDownTimer(10000, 1) {
        public void onTick(long millisUntilFinished) {
            textViewTimer.setText("" + millisUntilFinished / 1000
                    + ":" + millisUntilFinished % 1000);
        }

        public void onFinish() {
            textViewTimer.setText("0:000");
        }
    }.start();


}

我不会为隐私编写其他代码的其余部分,但这是感兴趣的部分;)

4

2 回答 2

1

已编辑

您可以将链接保存到您的 CountDownTimer 并在您想要的任何地方使用它。对于您的问题,您可以有两个布尔变量:timerStarts- 负责启动计时器一次,timerProcessing- 负责检查计时器是否仍在处理。

像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtCount = (TextView)findViewById(R.id.textView1);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.button1);

boolean timerProcessing = false;
boolean timerStarts = false;

final TextView textViewTimer = (TextView)findViewById(R.id.textView2);
//Saving link to timer object
final CountDownTimer timer = new CountDownTimer(10000, 1) {
    public void onTick(long millisUntilFinished) {
        textViewTimer.setText("" + millisUntilFinished / 1000
            + ":" + millisUntilFinished % 1000);
    }

    public void onFinish() {
        textViewTimer.setText("0:000");
        timerProcessing = false;
    }
});

btnCount.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        //start timer once when button first click
        if (!timerStarts){
            timer.start();
            timerStarts = true;
            timerProcessing = true;
        }

        if (timerProcessing){
            count++;
            txtCount.setText(String.valueOf(count));
        }
}
});
}

或者您可以有一个变量(例如 timerStarts),在按钮侦听器中检查它并在计时器停止时设置为 false,但是当您在计时器停止后单击按钮时,它将重新开始。希望能帮助到你。

于 2013-10-06T23:45:14.510 回答
0

如果我理解正确,您希望在完成Button后停止计算点击次数CoundTownTimer。为此,您可以Button在计时器完成时禁用。

 public void onFinish() {
    textViewTimer.setText("0:000");
    btnCount.setEnabled(false);   // Add this line here
 }

如果由于某种原因对您不起作用,那么您可以使用boolean变量来决定是否添加到您的计数中。

boolean addToCount = true;  // Create a variable

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtCount = (TextView)findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount = (Button)findViewById(R.id.button1);
    final TextView textViewTimer = (TextView)findViewById(R.id.textView2);
    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            count = (addToCount) ? count++ : count;  // Add 1 to count if 
                                                     // addToCount is true
            txtCount.setText(String.valueOf(count));
        }

    new CountDownTimer(10000, 1) {
    public void onTick(long millisUntilFinished) {
        textViewTimer.setText("" + millisUntilFinished / 1000
                + ":" + millisUntilFinished % 1000);
    }

    public void onFinish() {
        textViewTimer.setText("0:000");
        addToCount = false;     // Change variable so count doesn't increase
    }
   }.start();
于 2013-10-07T02:20:51.630 回答