繁体   English   中英

无法访问测验的 CountDownTimer 我需要它是怎样的

[英]CountDownTimer for quiz can't be accessed how I need it to be

我正在写一个小数学测验,每个级别有 10 个问题。 我正在尝试编写一个 10 秒计时器,它具有以下功能:

  • 当它用完并重新启动计时器时移动到下一个问题
  • 输入问题时重新启动

我的问题是,无论我如何编写此代码,我都无法从程序的“onFinish()”function 和 rest 访问计时器。 例如“onCorrect()”我需要能够取消并重新启动在“onCreate()”中初始化的现有计时器,但是我不能通过 MyCountDownTimer class 或 function 访问该计时器(我已经尝试使用两者都没有有用。)

我的代码在下面带有注释,以显示我需要计时器在哪里工作。

package com.example.myapplication;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.CountDownTimer;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import java.util.Random;


public class Level2 extends AppCompatActivity {
    int score = 0;
    int q = 1;
    TextView value;
    TextView q_num;
    TextView timer_label = findViewById(R.id.Timer);

    /*public class MyCountDownTimer extends CountDownTimer {
        TextView timer_label = findViewById(R.id.Timer);
        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }


        @Override
        public void onFinish() {
            onDone();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            int temp = (int)millisUntilFinished/1000;
            //do what ever You want
            timer_label.setText(String.format(getResources().getString(R.string.timer_string),String.valueOf(temp)));


        }
    }
    @Override

     */
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level2);


        q = 1;
        //MyCountDownTimer t = new MyCountDownTimer(11000,1000);

        Button b1 = findViewById(R.id.option1);
        Button b2 = findViewById(R.id.option2);
        Button b3 = findViewById(R.id.option3);
        Button b4 = findViewById(R.id.option4);

        Random first = new Random();
        int upperbound = 100;
        int random_id = first.nextInt(upperbound);

        selectData(random_id);


        //
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); //bellow setSupportActionBar(toolbar);
        getSupportActionBar().setCustomView(R.layout.action_layout);
        //Gives action bar style

        value = findViewById(R.id.score_value);
        q_num = findViewById(R.id.question_number);


        b1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String ans = (String) b1.getText();
                onCheck(ans,score);
            }
        });
        b2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String ans = (String) b2.getText();
                onCheck(ans,score);
            }
        });
        b3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String ans = (String) b3.getText();
                onCheck(ans,score);
            }
        });
        b4.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String ans = (String) b4.getText();
                onCheck(ans,score);
            }
        });


    }



    public void onCheck(String ans, int score) {
        TextView ans_data = findViewById(R.id.answer_data);
        String correct = (String) ans_data.getText();
        if (ans.contains(correct)) {
            onCorrect(score);
        }
        else{

            onWrong();
        }
    }


    public void onCorrect(int temp){
        //t.cancel()
        //t.start()
        temp++;
        score = temp;
        q++;
        if(q >= 11){
            if(score >2){
                complete(score);
            }
            else{
                Intent i = new Intent(Level2.this, ne.class);
                startActivity(i);
            }
        }
        value.setText(String.valueOf(score));
        q_num.setText(String.valueOf(q));
        Random first = new Random();
        int upperbound = 100;
        int random_id = first.nextInt(upperbound);
        selectData(random_id);
    }

    public void onDone(){
        //t.start()
        q++;
        if(q>=11){
            if(score >2){
                complete(score);
            }
            else{
                Intent i = new Intent(Level2.this, ne.class);
                startActivity(i);
            }
        }
        else{
            q_num.setText(String.valueOf(q));
            Random first = new Random();
            int upperbound = 100;
            int random_id = first.nextInt(upperbound);
            selectData(random_id);
        }
    }

    public void onWrong(){
        //t.cancel()
        //t.start
        q++;
        if(q>=11){
            if(score >2){
                complete(score);
            }
            else{
                Intent i = new Intent(Level2.this, ne.class);
                startActivity(i);
            }
        }
        else{
            q_num.setText(String.valueOf(q));
            Random first = new Random();
            int upperbound = 100;
            int random_id = first.nextInt(upperbound);
            selectData(random_id);
        }

以供参考;

  • onCheck() 检查答案是否正确
  • onCorrect() 是对的
  • onWrong() 错误
  • onDone() 是计时器用完的时间

我的问题的根源是我无法将 t 放入 onDone 中,因为它被 MyCountDownTimer class 调用,它无法访问 onCreate 中调用的 t。

将 onCreate() 之外的 MyCountDownTimer t 声明为全局变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM