我正在尝试在我的代码中插入一个CountDown Timer,但我不知道我做错了什么,因为在红色标记的任何行中都有很多错误。
我需要一个以00:15秒开始直到00:00的倒计时。我搜索了任何代码(包括Android开发人员的代码,但没有任何效果。
这是我的代码,我不知道在哪里插入倒数计时器代码。 任何人都可以帮助我?
提前致谢。
package com.pandaeducation.masterofmath;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class GameActivity extends Activity implements View.OnClickListener {
int correctAnswer;
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;
TextView textObjectPartA;
TextView textObjectPartB;
TextView textObjectScore;
TextView textObjectLevel;
int currentScore = 0;
int currentLevel = 1;
private int answerGiven;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//The next line loads our UI design to the screen
setContentView(R.layout.activity_game);
//*Here we get a working object based on either the button or TextView class
// and base as well as link our new objects
// directly to the appropriate UI elements that we created previously*/
textObjectPartA = (TextView) findViewById(R.id.textPartA);
textObjectPartB = (TextView) findViewById(R.id.textPartB);
textObjectScore = (TextView) findViewById(R.id.textScore);
textObjectLevel = (TextView) findViewById(R.id.textLevel);
buttonObjectChoice1 = (Button) findViewById(R.id.buttonChoice1);
buttonObjectChoice2 = (Button) findViewById(R.id.buttonChoice2);
buttonObjectChoice3 = (Button) findViewById(R.id.buttonChoice3);
//Now we use the setText method of the class on our objects
//to show our variable values on the UI elements.
buttonObjectChoice1.setOnClickListener(this);
buttonObjectChoice2.setOnClickListener(this);
buttonObjectChoice3.setOnClickListener(this);
}//onCreate ends here
@Override
public void onClick(View view) {
//declare a new int to be used in all the cases
int answerGiven = 0;
switch (view.getId()) {
case R.id.buttonChoice1:
//initialize a new int with the value contained in buttonObjectChoice1
//remember we put it there ourselves previously
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
break;
case R.id.buttonChoice2:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());
break;
case R.id.buttonChoice3:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
break;
}
updateScoreAndLevel(answerGiven);
setQuestion();
}
void setQuestion() {
//Generate the parts of the question
int numberRange = currentLevel * 3;
Random randInt = new Random();
int partA = randInt.nextInt(numberRange);
partA++;//don't want a zero value
int partB = randInt.nextInt(numberRange);
partB++;//dont't want a zero value
correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer - 2;
int wrongAnswer2 = correctAnswer + 2;
textObjectPartA.setText("" + partA);
textObjectPartB.setText("" + partB);
//set the multichoice buttons
//A number between 0 and 2
int buttonLayout = randInt.nextInt(3);
switch (buttonLayout) {
case 0:
buttonObjectChoice1.setText("" + correctAnswer);
buttonObjectChoice2.setText("" + wrongAnswer1);
buttonObjectChoice3.setText("" + wrongAnswer2);
break;
case 1:
buttonObjectChoice2.setText("" + correctAnswer);
buttonObjectChoice3.setText("" + wrongAnswer1);
buttonObjectChoice1.setText("" + wrongAnswer2);
break;
case 2:
buttonObjectChoice3.setText("" + correctAnswer);
buttonObjectChoice1.setText("" + wrongAnswer1);
buttonObjectChoice2.setText("" + wrongAnswer2);
break;
}
}
void updateScoreAndLevel(int answerGiven) {
if (isCorrect(answerGiven)) {
for (int i = 1; i <= currentLevel; i++) {
currentScore = currentScore + i;
}
currentLevel++;
} else {
currentScore = 0;
currentLevel = 1;
}
//Actually update the two TextViews
textObjectScore.setText("Score: " + currentScore);
textObjectLevel.setText("Level: " + currentLevel);
}
boolean isCorrect(int answerGiven) {
boolean correctTrueOrFalse;
if (answerGiven == correctAnswer) {//WAY!
Toast.makeText(getApplicationContext(), "Well Done!", Toast.LENGTH_LONG).show();
correctTrueOrFalse = true;
} else {//Uh-oh!
Toast.makeText(getApplicationContext(), "Duh! Sorry", Toast.LENGTH_LONG).show();
correctTrueOrFalse = false;
}
return correctTrueOrFalse;
}
}
答案 0 :(得分:0)
以下是使用处理程序添加倒计时功能的步骤。 创建一个扩展Runnable的类MyTimer,并在其中添加以下内容 声明变量
public MyTimer(Handler handler) {
this.handler = handler;
// timer gets 15 sec or 15000 milliseconds
this.timeRemaining = 15000;
}
构造
@Override
public void run() {
if (!isKilled) {
Log.d("TAG", "run was called, time reamaining " + timeRemaining / 1000);
timeRemaining = timeRemaining - 1000;
if (timeRemaining >= 0) {
handler.postDelayed(this, 1000);
} else {
//do something like calling a method
}
}
}
覆盖run方法
private Handler mCurrentHandler;
private MyTimer timer;
在MainActivity中,将以下内容添加为类变量
mCurrentHandler = new Handler();
timer = new MyTimer(mCurrentHandler);
在onCreate方法中添加
timer.setTimeRemaining(MyTimer.convertToMilliseconds(input));
timer.start();
最后在任何地方添加以下内容,以便开始倒计时
{{1}}