第二次点击“开始”后,倒数计时器不可用

时间:2018-09-06 16:48:18

标签: android countdowntimer

我正在尝试构建一个非常简单的游戏,其中您必须在一分钟内尽快单击弹出按钮(弹出按钮为1,弹出按钮为0,不弹出按钮)(以在其中获得尽可能多的重击)大体时间)。我可以通过点击“开始”按钮来启动我的应用并启动计数器,

但是,如果由于某种原因我在几秒钟后错误地再次单击“开始”,则计数器会出现问题,并且会在第一次倒数和第二次倒数之间快速切换。我的代码有什么问题?另外,当倒数计时器自动变为0(或“完成!”)时,我也不知道如何使节拍计数器变为0。你能帮忙吗?

非常感谢您的光临!

这是我的代码:

MainActivity.java

GET

activity_main.xml

package com.example.android.whack_a_lock_022;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    int cl = 0;
    int nl = 0;
    int sl = 0;
    int whacks = 0;
    int maxwhacks = 0;
    float random0to1;
    private CountDownTimer countDownTimer;
    private Button start;
    private Button cancel;
    private TextView time;
    Random r;

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

        r = new Random();

        Button clButton = findViewById(R.id.cl);
        Button nlButton = findViewById(R.id.nl);
        Button slButton = findViewById(R.id.sl);

        View.OnClickListener btnClickListener = new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                switch(v.getId()) {
                    case R.id.startButton :
                        start();
                        break;
                    case R.id.cancelButton :
                        cancel();
                        break;
                }
            }
        };

        clButton.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cl == 1) {
                    cl = 0;
                    TextView CapsLock = findViewById(R.id.cl);
                    CapsLock.setText("0");
                    whacks++;
                    TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
                    NumberOfWhacks.setText(String.valueOf(whacks));
                    if (whacks > maxwhacks) {
                        maxwhacks = whacks;
                        TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
                        NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
                    }

                    pop();
                }
            }
        });

        nlButton.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (nl == 1) {
                    nl = 0;
                    TextView NumLock = findViewById(R.id.nl);
                    NumLock.setText("0");
                    whacks++;
                    TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
                    NumberOfWhacks.setText(String.valueOf(whacks));
                    if (whacks > maxwhacks) {
                        maxwhacks = whacks;
                        TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
                        NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
                    }

                    pop();
                }
            }
        });

        slButton.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sl == 1) {
                    sl = 0;
                    TextView ScrollLock = findViewById(R.id.sl);
                    ScrollLock.setText("0");
                    whacks++;
                    TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
                    NumberOfWhacks.setText(String.valueOf(whacks));
                    if (whacks > maxwhacks) {
                        maxwhacks = whacks;
                        TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
                        NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
                    }

                    pop();
                }
            }
        });

        // Capture our button from layout
        start = (Button) findViewById(R.id.startButton);
        start.setOnClickListener(btnClickListener);
        cancel = (Button) findViewById(R.id.cancelButton);
        cancel.setOnClickListener(btnClickListener);
        time = (TextView) findViewById(R.id.time);

        // Register the onClick listener with the implementation above

    }

    public float numAlea() {
        return r.nextFloat();
    }

    private void start () {

        time.setText("60");

        countDownTimer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                time.setText("" + millisUntilFinished / 1000);
            }

            @Override
            public void onFinish(){
                time.setText("Done !");
            }
        };
        countDownTimer.start();

        pop();

    }

    private void pop(){
        // Escolhe e "POPa" o botão

        // Gera número aleatório

        random0to1 = numAlea();

        if (random0to1 <= (1F / 3)) {

            cl = 1;
            Button clButton = (Button) findViewById(R.id.cl);
            clButton.setText("1");

        } else if (random0to1 <= 2F / 3) {
            nl = 1;
            Button nlButton = (Button) findViewById(R.id.nl);
            nlButton.setText("1");
        } else if (random0to1 <= 1F) {
            sl = 1;
            Button slButton = (Button) findViewById(R.id.sl);
            slButton.setText("1");
        }
    }

    private void cancel() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }

        //Põe botões a zeros

        cl = 0;
        Button clButton = (Button) findViewById(R.id.cl);
        clButton.setText("0");

        nl = 0;
        Button nlButton = (Button) findViewById(R.id.nl);
        nlButton.setText("0");

        sl = 0;
        Button slButton = (Button) findViewById(R.id.sl);
        slButton.setText("0");

        whacks = 0;
        TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
        NumberOfWhacks.setText("0");

    }

}

1 个答案:

答案 0 :(得分:0)

如果您只是继续复制相同的CountDownTimer并让它的多个实例一次运行,那就可以了。在重新启动计时器之前,您需要检查计时器是否正在运行。有两种方法可以做到这一点。

方法1:布尔值

vm.InsuranceId.subscribe(function (newValue) {
    //doing something here
}

方法2:空检查

public CountDownTimer countDownTimer;
public boolean isTimerRunning = false;

private void start () {
    if (!isTimerRunning) {
        time.setText("60");

        countDownTimer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                time.setText("" + millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                isTimerRunning = false;
                time.setText("Done !");
            }
        };
        countDownTimer.start();
        isTimerRunning = true;

        pop();
    }
}