如何阻止计时器显示在我的其他级别?

时间:2017-04-03 04:07:55

标签: android android-studio countdowntimer

我制作了3个等级的游戏,简单,正常,而且很难。

容易在超时活动出现前300秒, 正常有600秒, 很难有900秒

例如,我选择了轻松但我输了,所以我回去选择接下来的等级。我选择正常,但计时器页面很容易显示。

如何阻止TIMER弹出其他级别。

我错过了什么

级别的活动:

public class EasyAct extends AppCompatActivity {
    Button b1, b2, b3, b4;
    TextView tv1, tv2, tv3, tv4;
    int score =0;
    TriviaQuizHelper1 triviaQuizHelper1;
    TriviaQuestion1 currentQuestion;
    List<TriviaQuestion1> list;
    int qid = 0;
    int level = 1;

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

        tv1 = (TextView) findViewById(R.id.triviaQuestion);
        tv2 = (TextView) findViewById(R.id.triviTimer);
        b1 = (Button) findViewById(R.id.buttonA);
        b2 = (Button) findViewById(R.id.buttonB);
        b3 = (Button) findViewById(R.id.buttonC);
        b4 = (Button) findViewById(R.id.buttonD);
        tv3 = (TextView) findViewById(R.id.level);
        tv4 = (TextView) findViewById(R.id.score);
        CountDownTimer countDownTimer = new CountDownTimer(300 * 1000, 1000) {
            @Override
            public void onTick(long l) {
                tv2.setText("Timer:" + l / 1000 + " " + "sec");
            }

            @Override
            public void onFinish() {
                tv2.setText("TIME OUT");
                Intent intent = new Intent(EasyAct.this, timeup.class);
                startActivity(intent);
            }
        };
        countDownTimer.start();
        triviaQuizHelper1 = new TriviaQuizHelper1(this);
        SQLiteDatabase sqLiteDatabase = triviaQuizHelper1.getWritableDatabase();
        triviaQuizHelper1.allQuestion1();
        list = triviaQuizHelper1.getAllQuestion();
        currentQuestion = list.get(qid);
        view();
    }

    public void view() {
        tv1.setText(currentQuestion.getQuestion());
        b1.setText(" "+currentQuestion.getOpta());
        b2.setText(" "+currentQuestion.getOptb());
        b3.setText(" "+currentQuestion.getOptc());
        b4.setText(" "+currentQuestion.getOptd());
        tv3.setText("Level:"+level);
        tv4.setText("Score:"+score);
    }


    public void ButtonA(View view) {

        if (b1.getText().toString().equals(" "+currentQuestion.getAnswer())){
            score+=300;
            if(qid<19){
                qid++;
                currentQuestion = list.get(qid);
                view();
            }else{
                Intent intent=new Intent(EasyAct.this,ResultWon.class);
                startActivity(intent);
                finish();
            }
        }
        else {
            Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
            Bundle b=new Bundle();
            b.putInt("xscore",score);
            intent.putExtras(b);
            startActivity(intent);
            finish();
        }
    }
    public void ButtonB(View view) {
        if (b2.getText().toString().equals(" "+currentQuestion.getAnswer())){
            score+=300;
            if(qid<19){
                qid++;
                currentQuestion = list.get(qid);
                view();
            }else{
                Intent intent=new Intent(EasyAct.this,ResultWon.class);
                startActivity(intent);
                finish();
            }
        }
        else {
            Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
            Bundle b=new Bundle();
            b.putInt("xscore",score);
            intent.putExtras(b);
            startActivity(intent);
            finish();
        }
    }
    public void ButtonC(View view) {
        if (b3.getText().toString().equals(" "+currentQuestion.getAnswer())){
            score+=300;
            if(qid<19){
                qid++;
                currentQuestion = list.get(qid);
                view();
            }else{
                Intent intent=new Intent(EasyAct.this,ResultWon.class);
                startActivity(intent);
                finish();
            }
        }
        else {
            Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
            Bundle b=new Bundle();
            b.putInt("xscore",score);
            intent.putExtras(b);
            startActivity(intent);
            finish();
        }
    }
    public void ButtonD(View view) {
        if (b4.getText().toString().equals(" "+currentQuestion.getAnswer())){
            score+=300;
            if(qid<19){
                qid++;
                currentQuestion = list.get(qid);
                view();
            }else{
                Intent intent=new Intent(EasyAct.this,ResultWon.class);
                startActivity(intent);
                finish();
            }
        }
        else {
            Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
            Bundle b=new Bundle();
            b.putInt("xscore",score);
            intent.putExtras(b);
            startActivity(intent);
            finish();
        }
    }
    @Override
    public void onBackPressed() {
        Intent a = new Intent(Intent.ACTION_MAIN);
        a.addCategory(Intent.CATEGORY_HOME);
        a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(a);
        finish();

    }
}

2 个答案:

答案 0 :(得分:1)

您需要在切换活动之前停止计时器。如果不这样做,新活动将显示在屏幕上,但旧活动的计时器仍然可以触发。要明确地执行此操作,您需要对可以访问onCreate()方法的外部的计时器的引用。一种简单的方法是将CountDownTimer声明为类字段。然后,您可以在此类中的任何位置访问变量。

public class EasyAct extends AppCompatActivity {
    Button b1, b2, b3, b4;
    TextView tv1, tv2, tv3, tv4;
    int score =0;
    TriviaQuizHelper1 triviaQuizHelper1;
    TriviaQuestion1 currentQuestion;
    List<TriviaQuestion1> list;
    // declaring CountDownTimer as a class field
    CountDownTimer countDownTimer;
    int qid = 0;
    int level = 1;


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

        tv1 = (TextView) findViewById(R.id.triviaQuestion);
        tv2 = (TextView) findViewById(R.id.triviTimer);
        b1 = (Button) findViewById(R.id.buttonA);
        b2 = (Button) findViewById(R.id.buttonB);
        b3 = (Button) findViewById(R.id.buttonC);
        b4 = (Button) findViewById(R.id.buttonD);
        tv3 = (TextView) findViewById(R.id.level);
        tv4 = (TextView) findViewById(R.id.score);

        // initializing the CountDownTimer instance
        countDownTimer = new CountDownTimer(300 * 1000, 1000) {
            @Override
            public void onTick(long l) {
                tv2.setText("Timer:" + l / 1000 + " " + "sec");
            }

然后,您可以在致电countDownTimer.stop()之前致电startActivity()

答案 1 :(得分:0)

您可以将countDownTimer作为全局变量,并在启动新活动时取消它,如下所示:

public class EasyAct extends AppCompatActivity {
        Button b1, b2, b3, b4;
        TextView tv1, tv2, tv3, tv4;
        int score =0;
        TriviaQuizHelper1 triviaQuizHelper1;
        TriviaQuestion1 currentQuestion;
        List<TriviaQuestion1> list;
        int qid = 0;
        int level = 1;
        private CountDownTimer countDownTimer; // declare as a global variable

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

            tv1 = (TextView) findViewById(R.id.triviaQuestion);
            tv2 = (TextView) findViewById(R.id.triviTimer);
            b1 = (Button) findViewById(R.id.buttonA);
            b2 = (Button) findViewById(R.id.buttonB);
            b3 = (Button) findViewById(R.id.buttonC);
            b4 = (Button) findViewById(R.id.buttonD);
            tv3 = (TextView) findViewById(R.id.level);
            tv4 = (TextView) findViewById(R.id.score);
            // initialize the timer
            countDownTimer = new CountDownTimer(300 * 1000, 1000) {
                @Override
                public void onTick(long l) {
                    tv2.setText("Timer:" + l / 1000 + " " + "sec");
                }

                @Override
                public void onFinish() {
                    tv2.setText("TIME OUT");
                    Intent intent = new Intent(EasyAct.this, timeup.class);
                    startActivity(intent);
                }
            };
            countDownTimer.start();
            triviaQuizHelper1 = new TriviaQuizHelper1(this);
            SQLiteDatabase sqLiteDatabase = triviaQuizHelper1.getWritableDatabase();
            triviaQuizHelper1.allQuestion1();
            list = triviaQuizHelper1.getAllQuestion();
            currentQuestion = list.get(qid);
            view();
        }

        public void view() {
            tv1.setText(currentQuestion.getQuestion());
            b1.setText(" "+currentQuestion.getOpta());
            b2.setText(" "+currentQuestion.getOptb());
            b3.setText(" "+currentQuestion.getOptc());
            b4.setText(" "+currentQuestion.getOptd());
            tv3.setText("Level:"+level);
            tv4.setText("Score:"+score);
        }


        public void ButtonA(View view) {

            if (b1.getText().toString().equals(" "+currentQuestion.getAnswer())){
                score+=300;
                if(qid<19){
                    qid++;
                    currentQuestion = list.get(qid);
                    view();
                }else{
                    Intent intent=new Intent(EasyAct.this,ResultWon.class);
                    startActivity(intent);
                    countDownTimer.cancel();
                    finish();
                }
            }
            else {
                Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
                countDownTimer.cancel();
                Bundle b=new Bundle();
                b.putInt("xscore",score);
                intent.putExtras(b);
                startActivity(intent);
                finish();
            }
        }
        public void ButtonB(View view) {
            if (b2.getText().toString().equals(" "+currentQuestion.getAnswer())){
                score+=300;
                if(qid<19){
                    qid++;
                    currentQuestion = list.get(qid);
                    view();
                }else{
                    Intent intent=new Intent(EasyAct.this,ResultWon.class);
                    countDownTimer.cancel();
                    startActivity(intent);
                    finish();
                }
            }
            else {
                Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
                countDownTimer.cancel();
                Bundle b=new Bundle();
                b.putInt("xscore",score);
                intent.putExtras(b);
                startActivity(intent);
                finish();
            }
        }
        public void ButtonC(View view) {
            if (b3.getText().toString().equals(" "+currentQuestion.getAnswer())){
                score+=300;
                if(qid<19){
                    qid++;
                    currentQuestion = list.get(qid);
                    view();
                }else{
                    Intent intent=new Intent(EasyAct.this,ResultWon.class);
                    countDownTimer.cancel();
                    startActivity(intent);
                    finish();
                }
            }
            else {
                Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
                countDownTimer.cancel();
                Bundle b=new Bundle();
                b.putInt("xscore",score);
                intent.putExtras(b);
                startActivity(intent);
                finish();
            }
        }
        public void ButtonD(View view) {
            if (b4.getText().toString().equals(" "+currentQuestion.getAnswer())){
                score+=300;
                if(qid<19){
                    qid++;
                    currentQuestion = list.get(qid);
                    view();
                }else{
                    Intent intent=new Intent(EasyAct.this,ResultWon.class);
                    countDownTimer.cancel();
                    startActivity(intent);
                    finish();
                }
            }
            else {
                Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
                countDownTimer.cancel();
                Bundle b=new Bundle();
                b.putInt("xscore",score);
                intent.putExtras(b);
                startActivity(intent);
                finish();
            }
        }
        @Override
        public void onBackPressed() {
            Intent a = new Intent(Intent.ACTION_MAIN);
            countDownTimer.cancel();
            a.addCategory(Intent.CATEGORY_HOME);
            a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(a);
            finish();
        }
    }