2016-10-25 90 views
-1

我想在我的代碼中插入一個CountDown計時器,但我不知道我在做什麼錯誤,因爲在標有紅色的任何行中有很多錯誤。插入倒數計時器的問題[android studio]

我需要一個從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

以下是添加使用處理器倒計時功能的步驟。 創建一個擴展了Runnable類MyTimer和裏面添加以下 聲明變量

private long timeRemaining; 
private Handler handler; 

構造

public MyTimer(Handler handler) { 
     this.handler = handler; 
     // timer gets 15 sec or 15000 milliseconds 
     this.timeRemaining = 15000; 
    } 

覆蓋run方法

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

在MainActivity添加以下作爲類變量

private Handler mCurrentHandler; 
private MyTimer timer; 

在onCreate方法添加

mCurrentHandler = new Handler(); 
timer = new MyTimer(mCurrentHandler); 

最後補充哪裏ü希望,這樣的倒計時開始

timer.setTimeRemaining(MyTimer.convertToMilliseconds(input)); 
timer.start(); 
以下