Java程序中的计时器,倒计时

时间:2013-11-29 09:36:30

标签: java android timer countdown

我想为我的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;
    }

}

2 个答案:

答案 0 :(得分:2)

  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();
    }
    
  3. 您的 .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>
    

答案 1 :(得分:1)

new CountDownTimer(60000, 1000) {

public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     //here you can have your logic to set text to edittext
}

public void onFinish() {
    mTextField.setText("done!");
}
}.start();

这是example

请参阅此link了解倒计时器。