2016-10-25 2 views
-1

카운트 다운 타이머를 코드에 삽입하려고하는데, 빨간색으로 표시된 모든 줄에 많은 오류가 있기 때문에 잘못된 것을 알지 못합니다. .카운트 다운 타이머를 삽입하는 데 문제가 있습니다. [안드로이드 스튜디오]

00:15 초부터 00:00까지 시작되는 카운트 다운이 필요합니다. 나는 어떤 코드 (개발자 안드로이드의 코드를 포함 검색,하지만 아무것도 잘 작동합니다.이 아래에있는 내 코드는

나는 카운트 다운 타이머 코드를 삽입 위치를 알 수 없습니다. 수 누구든지 저를하는 데 도움이?

미리 감사드립니다. 다음

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

재정의 실행 방법

MainActivity에서
@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 
      } 
     } 
    } 

는에서 onCreate 방법에

private Handler mCurrentHandler; 
private MyTimer timer; 

추가 클래스 변수로 다음을 추가

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

마지막으로 유 카운트 다운

timer.setTimeRemaining(MyTimer.convertToMilliseconds(input)); 
timer.start(); 
을 시작 너무 원하는 목적지에 다음을 추가
관련 문제