活动结束后计数器计时器也不会停止

时间:2018-12-30 20:27:37

标签: android countdowntimer

我有一个测验应用程序,该应用程序具有用于整个游戏活动的计时器,您可以在其中回答20个问题,每个问题有10秒钟..

分配的20结束后,它将带您到显示您的分数的结果活动。即使在调用计分活动后,也会显示来自主要活动的吐司,并且计分活动将再次打开。

int score = 0;
int unanswer = 0;
int questionasked = 1;
int maxquestion = 20;
TextView first, operator, second, timeview;
Button A, B, C, D;
int ans = 0;
String t;
DecimalFormat df;
CountDownTimer mCountDownTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timeview = findViewById(R.id.time);
    A = findViewById(R.id.A);
    A.setOnClickListener(this);
    B = findViewById(R.id.B);
    B.setOnClickListener(this);
    C = findViewById(R.id.C);
    C.setOnClickListener(this);
    D = findViewById(R.id.D);
    D.setOnClickListener(this);
    first = findViewById(R.id.first);
    second = findViewById(R.id.second);
    operator = findViewById(R.id.operator);
    df = new DecimalFormat("###.##");
    starTimer();
    fillValue();

}
  

主要活动代码

private void fillValue() {
    if (questionasked > maxquestion)
        showscore();
    questionasked++;
    int a = (int) (Math.random() * 50 + 1);
    int b = (int) (Math.random() * 50 + 1);
    int op = (int) (Math.random() * 4 + 1);
    int buttonoption = (int) (Math.random() * 4 + 1);
    char operatorcharter = '+';
    switch (op) {
        case 1:
            ans = a + b;
            operatorcharter = '+';
            break;
        case 2:
            ans = a - b;
            operatorcharter = '-';
            break;
        case 3:
            ans = a * b;
            operatorcharter = 'X';
            break;
        case 4:
            ans = (a) / b;
            operatorcharter = '/';
            break;
        //default:
        //Toast.makeText(this,op+"",Toast.LENGTH_SHORT).show();
    }
    //Toast.makeText(this,a+" "+operatorcharter+" "+b+" "+ans+" "+buttonoption,Toast.LENGTH_LONG).show();
    operator.setText(operatorcharter + "");
    first.setText(a + "");
    second.setText(b + "");
    t = df.format(ans);
    int temp = 0;
    switch (buttonoption) {
        case 1:
            A.setText(t);
            break;
        case 2:
            B.setText(t);
            break;
        case 3:
            C.setText(t);
            break;
        case 4:
            D.setText(t);
            break;
        //default:
        // Toast.makeText(this,buttonoption+" butt",Toast.LENGTH_SHORT).show();
    }
    for (int i = 1; i <= 4; i++) {
        if (i == buttonoption)
            continue;
        temp = (int) (Math.random() * ans + 1);
        if (temp == ans) ;
        temp += 5;
        String temp1 = df.format(temp);
        if (i == 1)
            A.setText(temp1 + "");
        else if (i == 2)
            B.setText(temp1 + "");
        else if (i == 3)
            C.setText(temp1 + "");
        else if (i == 4)
            D.setText(temp1 + "");
    }
    mCountDownTimer.start();
}

private void showscore() {
    Intent i = new Intent(this, final_activity.class);
    i.putExtra(Contact.maxquestion, maxquestion);
    i.putExtra(Contact.score, score);
    i.putExtra(Contact.unanswer, unanswer);
    mCountDownTimer.cancel();
    Toast.makeText(this, "new activity open", Toast.LENGTH_LONG).show();
    startActivity(i);
    this.finish();
}

@Override
public void onClick(View v) {
    Button b = (Button) v;
    String clickbuttonvalue = (String) b.getText();
    if (clickbuttonvalue.equals(t))
        score++;
    //Toast.makeText(this,score+"  "+clickbuttonvalue   ,Toast.LENGTH_LONG).show();
    mCountDownTimer.cancel();
    fillValue();
}


public void starTimer() {
    mCountDownTimer = new CountDownTimer(10000, 1000) {

        public void onTick(long millisUntilFinished) {
            timeview.setText("seconds remaining: " + millisUntilFinished / 1000);
            Toast.makeText(MainActivity.this, "new" + millisUntilFinished / 1000, Toast.LENGTH_LONG).show();
        }

        public void onFinish() {
            unanswer++;
            fillValue();
        }

    }.start();
}

}

即使在打开新活动并打开新活动检查方法showcore()之后,这也是我的代码 请帮忙!

1 个答案:

答案 0 :(得分:0)

您需要覆盖respect language direction方法,该方法将在活动完成时触发(不要忘记在onDestroy之后添加finish())。

startActivty()