2013-09-05 43 views
2

我工作在我的應用程序的android測驗我有QuestionActivity類。其中setQuestions()方法將來自數據庫的文本分配到按鈕textview中,並將score設置爲第一次。在下一個問題的下一個文本中被分配並且分數也被更新,無論該問題是錯誤的還是正確的。我想要一個自動計時器,當我們進入這個活動時開始。並在下一輪從一段時間再次開始,並繼續下降。我知道這一點,我必須創建與此調用相關的問題xml的textview。但是我不知道在這門課程中如何編寫代碼。定時器時間30秒。我張貼這個類的代碼。有人幫我。如何創建自動啓動計時器?

public class QuestionActivity extends Activity implements OnClickListener{ 

    private Question currentQ; 
    private GamePlay currentGame; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.question); 
       /** 
     * Configure current game and get question 
     */ 
     currentGame = ((CYKApplication)getApplication()).getCurrentGame(); 
     currentQ = currentGame.getNextQuestion(); 
     Button nextBtn1 = (Button) findViewById(R.id.answer1); 
     nextBtn1.setOnClickListener(this); 
     Button nextBtn2 = (Button) findViewById(R.id.answer2); 
     nextBtn2.setOnClickListener(this); 
     Button nextBtn3 = (Button) findViewById(R.id.answer3); 
     nextBtn3.setOnClickListener(this); 
     Button nextBtn4 = (Button) findViewById(R.id.answer4); 
     nextBtn4.setOnClickListener(this); 
     Button nextBtn5 = (Button) findViewById(R.id.answer5); 
     nextBtn5.setOnClickListener(this); 


     /** 
     * Update the question and answer options.. 
     */ 
     setQuestions(); 

    } 


    /** 
    * Method to set the text for the question and answers from the current games 
    * current question 
    */ 
    private void setQuestions() { 
     //set the question text from current question 
     String question = Utility.capitalise(currentQ.getQuestion()); 
     TextView qText = (TextView) findViewById(R.id.question); 
     qText.setText(question); 

     //set the available options 
     List<String> answers = currentQ.getQuestionOptions(); 
     TextView option1 = (TextView) findViewById(R.id.answer1); 
     option1.setText(Utility.capitalise(answers.get(0))); 

     TextView option2 = (TextView) findViewById(R.id.answer2); 
     option2.setText(Utility.capitalise(answers.get(1))); 

     TextView option3 = (TextView) findViewById(R.id.answer3); 
     option3.setText(Utility.capitalise(answers.get(2))); 

     TextView option4 = (TextView) findViewById(R.id.answer4); 
     option4.setText(Utility.capitalise(answers.get(3))); 

     int score = currentGame.getScore(); 
     String scr = String.valueOf(score); 
     TextView score1 = (TextView) findViewById(R.id.score); 
     score1.setText(scr); 
     } 


    @Override 
    public void onClick(View arg0) { 
     //Log.d("Questions", "Moving to next question"); 
     if(arg0.getId()==R.id.answer5) 
     { 
     new AlertDialog.Builder(this) 
     .setMessage("Are you sure?") 
     .setCancelable(true) 
     .setPositiveButton("Yes", 
     new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, 
     int id) { 
       finish(); 
       } 
      }).setNegativeButton("No", null).show(); 

       } 

     else 
     { 
      if(!checkAnswer(arg0)) return; 

     /** 
     * check if end of game 
     */ 
     if (currentGame.isGameOver()){ 
      //Log.d("Questions", "End of game! lets add up the scores.."); 
      //Log.d("Questions", "Questions Correct: " + currentGame.getRight()); 
      //Log.d("Questions", "Questions Wrong: " + currentGame.getWrong()); 
      Intent i = new Intent(this, EndgameActivity.class); 
      startActivity(i); 
      finish(); 
     } 
     else{ 
      Intent i = new Intent(this, QuestionActivity.class); 
      startActivity(i); 
      finish(); 
      } 
     } 
     } 



    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
     switch (keyCode) 
     { 
     case KeyEvent.KEYCODE_BACK : 
      return true; 
     } 

     return super.onKeyDown(keyCode, event); 
    } 


    /** 
    * Check if a checkbox has been selected, and if it 
    * has then check if its correct and update gamescore 
    */ 
    private boolean checkAnswer(View v) { 

     Button b=(Button) v; 
     String answer = b.getText().toString(); 

      //Log.d("Questions", "Valid Checkbox selection made - check if correct"); 
      if (currentQ.getAnswer().equalsIgnoreCase(answer)) 
      { 
       b.setBackgroundResource(R.drawable.answercolor); 
       //Log.d("Questions", "Correct Answer!"); 
       currentGame.incrementScore(); 
      } 
      else{ 
       b.setBackgroundResource(R.drawable.answercolorr); 
       //Log.d("Questions", "Incorrect Answer!"); 
       currentGame.decrementScore(); 
      } 
      return true; 
     } 

} 

回答

0

我想有一個自動定時器,當我們在這個 活動進入其啓動。並在下一輪從一段時間再次開始並繼續減少 。我知道爲此,我必須創建與此調用相關的問題xml 的textview。但我不知道在哪裏以及如何編寫代碼 這個類。定時器時間30秒。

可以使用CountDownTimer

new CountDownTimer(30000, 1000) { 

    public void onTick(long millisUntilFinished) { 
     mTextField.setText("seconds remaining: " + millisUntilFinished/1000); 
    } 

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

欲瞭解更多信息refer here

+0

i我沒有得到你。我認爲只有這個代碼不足以像我想要的那樣在我的遊戲中定時。你可以給我任何其他的解決方案嗎? –

+0

爲什麼它不夠?計時器將在一項活動中發揮作用... ...您的問題部分將得到重新整理...我建議您,因爲我做了類似的Q&A測驗應用程序.. – CRUSADER

+0

好的。但是,當我分配textview,我創建問題xml代替mTextField.setText(「」)。它顯示錯誤。 –

0

對於計時器有我用它每300ms的數據從手機發送Timer類achiev這一點。但是你需要一個計數器而不是一個定時器,因爲你使用一個線程處理程序和一個從counter = 30開始的整數,並且在處理程序中你減少了一個計數器 - 處理程序postat 1sec(1000ms)