在 CountdownTimer 上获取剩余时间并将剩余时间用作分数 android
Get remaining time on CountdownTimer and use the remaining time as a score android
所以我这里有测验应用程序和计时器。所以我想发生什么,例如我已经将计时器设置为 15 秒,如果用户在 5 秒内回答问题,我希望 10 秒的剩余时间变成 10 分,它将添加到之前的分数加上你的分数回答问题。所以现在我有这个...
if(savedInstanceState!=null){
//saved instance state data
int exScore = savedInstanceState.getInt("score");
scoreText.setText("Score: "+exScore);
}
Timer = new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tv_time.setText("" + millisUntilFinished / 1000);
int progress = (int) (millisUntilFinished / 150);
progressBar.setProgress(progress);
}
@Override
public void onFinish() {
progressBar.setProgress(0);
timeUp(context);
}
}.start();
这里是 onclick 的。如果用户回答正确它会自动加10分
public void onClick(View view) {
Button clicked = (Button) view;
int exScore = getScore();
if (clicked.getText().toString().equals(this.active_question.getAnswer()) ) {
if (this.questions.size() > 0) {
setQuestion(questions.poll());
scoreText.setText("Score: " + (exScore + 10))
} else {
CustomGameOver cdd = new CustomGameOver(PlayQuizActivity.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();
setHighScore();
Timer.cancel();
}
}
我不知道如何在 CountdownTimer 上获取剩余时间并在答案正确时将其添加为分数。谁能帮帮我。
只需使用 CountDownTimer 的 onTick 中的 millisUntilFinished
奖金将是millisUntilFinished/1000
P.S我认为你最好使用比 1000 更小的间隔,这样 ProgressBar 会看起来更平滑。
您需要做的就是在您的 MainActivity 中声明一个长变量 timeleft
。
long timeleft;
然后,当您创建一个新的计时器时,设置 "onTick" 覆盖以在每个 "onTick" 更新 timeleft 变量(在以下示例中为 1000 毫秒)
timer = new CountDownTimer(time, 1000) {
@Override
public void onTick(long millisecondsUntilFinished) {
timeleft = millisecondsUntilFinished;
}
}
每次您需要检查剩余时间时,您的应用程序都可以访问变量 timeleft
。
score = score + timeleft / 1000; // divide by 1000 to get seconds
请记住,如果您需要更新计时器,则必须取消它并创建一个新的计时器,并保留更新后的时间(以及相同的覆盖);
timeleft = timeleft + bonustime; // (if you want to add bonus time, remember has to be in milliseconds)
if( timer != null){ timer.cancel();} // better check first if the timer exists
timer = new CountDownTimer(timeleft, 1000) {
@Override
public void onTick(long millisecondsUntilFinished) {
timeleft = millisecondsUntilFinished;
}
所以我这里有测验应用程序和计时器。所以我想发生什么,例如我已经将计时器设置为 15 秒,如果用户在 5 秒内回答问题,我希望 10 秒的剩余时间变成 10 分,它将添加到之前的分数加上你的分数回答问题。所以现在我有这个...
if(savedInstanceState!=null){
//saved instance state data
int exScore = savedInstanceState.getInt("score");
scoreText.setText("Score: "+exScore);
}
Timer = new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tv_time.setText("" + millisUntilFinished / 1000);
int progress = (int) (millisUntilFinished / 150);
progressBar.setProgress(progress);
}
@Override
public void onFinish() {
progressBar.setProgress(0);
timeUp(context);
}
}.start();
这里是 onclick 的。如果用户回答正确它会自动加10分
public void onClick(View view) {
Button clicked = (Button) view;
int exScore = getScore();
if (clicked.getText().toString().equals(this.active_question.getAnswer()) ) {
if (this.questions.size() > 0) {
setQuestion(questions.poll());
scoreText.setText("Score: " + (exScore + 10))
} else {
CustomGameOver cdd = new CustomGameOver(PlayQuizActivity.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();
setHighScore();
Timer.cancel();
}
}
我不知道如何在 CountdownTimer 上获取剩余时间并在答案正确时将其添加为分数。谁能帮帮我。
只需使用 CountDownTimer 的 onTick 中的 millisUntilFinished
奖金将是millisUntilFinished/1000
P.S我认为你最好使用比 1000 更小的间隔,这样 ProgressBar 会看起来更平滑。
您需要做的就是在您的 MainActivity 中声明一个长变量 timeleft
。
long timeleft;
然后,当您创建一个新的计时器时,设置 "onTick" 覆盖以在每个 "onTick" 更新 timeleft 变量(在以下示例中为 1000 毫秒)
timer = new CountDownTimer(time, 1000) {
@Override
public void onTick(long millisecondsUntilFinished) {
timeleft = millisecondsUntilFinished;
}
}
每次您需要检查剩余时间时,您的应用程序都可以访问变量 timeleft
。
score = score + timeleft / 1000; // divide by 1000 to get seconds
请记住,如果您需要更新计时器,则必须取消它并创建一个新的计时器,并保留更新后的时间(以及相同的覆盖);
timeleft = timeleft + bonustime; // (if you want to add bonus time, remember has to be in milliseconds)
if( timer != null){ timer.cancel();} // better check first if the timer exists
timer = new CountDownTimer(timeleft, 1000) {
@Override
public void onTick(long millisecondsUntilFinished) {
timeleft = millisecondsUntilFinished;
}