簡體   English   中英

Java程序中的計時器,倒計時

[英]Timer in Java program, 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;
    }

}
  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>

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();

這是例子

有關倒數計時器,請參閱此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM