0
我有一個倒計時器,只要點擊生成表達式按鈕就會運行。在定時器內部,我已經使其工作,所以當定時器運行時它會自動點擊按鈕,以便顯示下一個表達式。當按鈕被點擊10次時,它應該開始一個新的活動。但是我遇到的問題是當新活動開始時,它始終開始相同的活動。所以我希望計時器在按鈕被點擊10次後立即停止。我試過countDownTimer.cancel();
只要點擊10次就停止計時器,但它似乎不起作用。爲什麼我的應用程序中的倒數計時器不停止?
這裏就是計時器代碼添加:
public void generate_Clicked(View v){ // When Generate button is clicked
if(gencount <10){
if (!timerHasStarted) {
countDownTimer.start();
timeText.setText(timeText.getText() + String.valueOf(startTime/1000));
timerHasStarted = true;
}
else {
countDownTimer.cancel();
timerHasStarted = false;
}
// Fetch your random question
String Rquestion = multiArray[ar.get(gencount)][0];
displayExpression.setText(Rquestion);
displayAnswer.setText("");
setAnswer.setText("?");
setHints.setText("");
count =0;
gencount++;
}else{
// countDownTimer.cancel(); //This doesn't seem to work
Intent i = new Intent(getApplicationContext(),Score.class);
startActivity(i);
ar.clear();
}
} //End of generate_Clicked.
繼承人的計時器代碼:
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
timeText.setText("Time's up!");
Button buttonGenerate = (Button) findViewById(R.id.random_gen);
buttonGenerate.performClick();
countDownTimer.cancel();
countDownTimer.start();
}
@Override
public void onTick(long millisUntilFinished) {
timeText.setText("" + millisUntilFinished/1000);
}
}
要澄清,當您打開Score活動時,它會使用倒數重新打開活動? – ataulm
我不確定,但你調用'countDownTimer.start();''onFinish' –
是的,它重新打開分數活動。 – sumr