gpt4 book ai didi

java - Java程序中的定时器,倒计时

转载 作者:行者123 更新时间:2023-12-01 23:24:00 28 4
gpt4 key购买 nike

我想为我的 Android 程序实现一个 java 倒计时器,但我不知道该怎么做。它应该从 60 到 0 计算时间,最后程序应该结束,但用户需要查看还剩多少时间,因此计时器应该始终可见。我设法实现了一个“计时器”来确定游戏接受输入的时间,但我不明白应该如何制作倒计时系统。这是我制作的程序的源文件。希望你们能帮助我提前谢谢,

  package com.example.mathcalcplus;

import java.util.Random;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PlayGame extends Activity implements OnClickListener {
ImageView image;
TextView question, answer, score, timeLeft;
int difLevel, operator1, operator2, finalAnswer=0, operation, scoreCount = 0, userAnswer=0;
final private int add = 0, sub = 1, div = 3, mul = 2;
String[] getOperation = { "+", "-", "*", "/"};
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnClear, btnEnter;
Random rand;
String getQuestion, userInput;
long start;
long end;
boolean running = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.play_game);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btn0 = (Button) findViewById(R.id.btn0);
btnClear = (Button) findViewById(R.id.clear);
btnEnter = (Button) findViewById(R.id.enter);
question = (TextView) findViewById(R.id.question);
answer = (TextView) findViewById(R.id.answer);
score = (TextView) findViewById(R.id.score);
image = (ImageView) findViewById(R.id.response);
timeLeft = (TextView) findViewById(R.id.timeLeft);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btn0.setOnClickListener(this);
btnClear.setOnClickListener(this);
btnEnter.setOnClickListener(this);
rand = new Random();
Bundle extras = getIntent().getExtras();
if(extras != null)
{
int passedLevel = extras.getInt("level", -1);
if(passedLevel>=0) difLevel = passedLevel;
}
image.setVisibility(View.INVISIBLE);
choseQuestion();
start = System.currentTimeMillis();
end = start + 30*1000;
}
public void run(){

}


@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.enter:
String getUserAnswer = answer.getText().toString();
if (!getUserAnswer.endsWith("?")){

userAnswer = Integer.parseInt(getUserAnswer.substring(1));

if (userAnswer == finalAnswer){
scoreCount++;
score.setText("Score=" + " " + scoreCount);
image.setImageResource(R.drawable.tick);
image.setVisibility(View.VISIBLE);
}
else{
scoreCount = 0;
score.setText("Score=" + " " + scoreCount);
image.setImageResource(R.drawable.cross);
image.setVisibility(View.VISIBLE);
}
finalAnswer =0;
choseQuestion();
getUserAnswer = "";
userAnswer = 0;
break;

}
else{
break;
}



case R.id.clear:
answer.setText("=?");
break;

default:
if (System.currentTimeMillis() < end){
int enteredNum = Integer.parseInt(v.getTag().toString());
if(answer.getText().toString().endsWith("?"))
answer.setText("="+enteredNum);
else
answer.append(""+enteredNum);
}
}

}
private void choseQuestion() {

answer.setText("=?");
operation = rand.nextInt(getOperation.length);
operator1 = getNumbers();
operator2 = getNumbers();
if (operation == sub){
while (operator2 > operator1){
operator1 = getNumbers();
operator2 = getNumbers();
}
}

if (operation == div){
while((double)operator1%(double)operator2 > 0 || (operator2 > operator1)){
operator1 = getNumbers();
operator2 = getNumbers();
}
}

switch(operation){
case add:
finalAnswer = operator1 + operator2;
break;

case sub:
finalAnswer = operator1 - operator2;
break;

case div:
finalAnswer = operator1 / operator2;
break;

case mul:
finalAnswer = operator1 * operator2;
break;
}
question.setText(operator1 + getOperation[operation] +operator2);
timeLeft.setText("Time :" + end);
}

private int getNumbers(){
int n = rand.nextInt(100) + 1;`enter code here`
return n;
}

}

最佳答案

  1. 声明您的 CountDownTimer 并设置时间范围(以毫秒为单位)。
  2. 调用 countDown 方法,您的 CountDownTimer 已准备就绪!

    private final static int time = 10000;
    private CountDownTimer timerCount;

    public void countDown(){
    countDown = (TextView) findViewById(R.id.countdown);

    timerCount = new CountDownTimer(time, 1000) {

    public void onTick(long millisUntilFinished) {
    long tmp = millisUntilFinished / 1000;
    countDown.setText(tmp + "");
    }

    public void onFinish() {
    // Do your stuff
    countDown.setText("0");
    }
    }.start();
    }

以及您的.xml 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/questionList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/menu"
android:orientation="vertical"
android:weightSum="4" >

<TextView
android:id="@+id/title"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:textSize="14sp"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/countdown"
android:layout_gravity="right"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" >

<requestFocus />
</TextView>

关于java - Java程序中的定时器,倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20282501/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com