2013-09-30 46 views
0

我有四個按鈕,當我點擊我的第一個應答按鈕有一些延遲,但在延遲時,當我點擊其他按鈕,他們都被選中。所以需要禁用其他按鈕,直到我的第一個答案按鈕被點擊,並且當用戶返回時,所有按鈕都需要被啓用。我怎麼解決這個問題?請幫忙!!如何禁用其他按鈕,直到在android中選擇延遲的第一個按鈕?

這裏是我的代碼: -

public class QuestionActivity extends Activity implements OnClickListener{ 
    private Question currentQ; 
    private GamePlay currentGame; 
    private CountDownTimer counterTimer; 

      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.question); 
       processScreen(); 
     } 
       /** 
     * Configure current game and get question 
     */ 
     private void processScreen() 
     { 
     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); 

     counterTimer=new CountDownTimer(15000, 1000) { 
      public void onFinish() {     
       if(currentGame.getRound()==20) 
       System.exit(0); 
       Toast.makeText(QuestionActivity.this, "Time's up", Toast.LENGTH_SHORT).show(); 
       currentGame.decrementScore(); 
       final Handler handle = new Handler(); 
       Runnable delay = new Runnable() { 
        public void run() { 
         processScreen(); 
        } 
       }; 
       handle.postDelayed(delay,3000); 
       } 

      public void onTick(long millisUntilFinished) { 
       TextView time = (TextView) findViewById(R.id.timers); 
       time.setText(""+millisUntilFinished/1000); 
           } 
     }; 
     counterTimer.start(); 
    } 


    @Override 
    public void onResume() { 
     super.onResume(); 
    } 


    @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()); 
      final Handler handle = new Handler(); 
      Runnable delay = new Runnable() { 
       public void run() { 
        startActivity(new Intent(QuestionActivity.this, EndgameActivity.class)); 
        finish(); 
       } 
      }; 
      handle.postDelayed(delay,2000); 

     } 
      else 
      { 
       final Handler handle = new Handler(); 
       Runnable delay = new Runnable() { 
        public void run() { 
        startActivity(new Intent(QuestionActivity.this, QuestionActivity.class)); 
        finish(); 
        } 
       }; 
       handle.postDelayed(delay,2000); 
         } 
        } 
       } 



    @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) { 
     final Button b = (Button) v; 
     String answer = b.getText().toString(); 
     counterTimer.cancel(); 
     b.setBackgroundResource(R.drawable.ans); 
     b.setEnabled(false); 
     //Log.d("Questions", "Valid Checkbox selection made - check if correct"); 
      if (currentQ.getAnswer().equalsIgnoreCase(answer)) 
       { 
       b.setBackgroundResource(R.drawable.ansgreen); 
       //Log.d("Questions", "Correct Answer!"); 
       Toast.makeText(QuestionActivity.this, "Correct Answer", Toast.LENGTH_SHORT).show(); 
       currentGame.incrementScore(); 
       } 

      else{ 
       b.setBackgroundResource(R.drawable.ansred); 
       //Log.d("Questions", "Incorrect Answer!"); 
       Toast.makeText(QuestionActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show(); 
       currentGame.decrementScore1(); 
          } 
      return true; 
     } 

} 
+0

使用帶收音機組的單選按鈕,一次只能選中1個。 – Raghunandan

+0

@Raghunandan我已經使用了按鈕,所以可以使用任何按鈕組。 – UchihaSasuke

回答

0

移動按鈕從onCreate申報到活動成員

private Button nextBtn1; 
private Button nextBtn2; 
private Button nextBtn3; 
private Button nextBtn4; 
private Button nextBtn5; 

將代碼放在onClick方法

nextBtn1.setEnabled(false); 
nextBtn2.setEnabled(false); 
nextBtn3.setEnabled(false); 
nextBtn4.setEnabled(false); 
nextBtn5.setEnabled(false); 
arg0.setEnabled(true); 

的beggining希望它有助於。

+0

偉大的工程,但你可以解釋我爲什麼發生這種情況。 – UchihaSasuke

+1

@ChandanKumar代碼只啓用了點擊的視圖(實際上是'arg0'),其餘的都被標記爲'disabled' –

+0

沒問題。回覆和回答非常好。如果我還有其他問題,我會向您詢問。聯繫。 – UchihaSasuke

0
Disable all other buttons other than button1 initially with button.setEnabled(false). 

Eg: button2.setEnabled(false); 
    button3.setEnabled(false); 
    button.setEnabled(false); 

Then in the onClick() method of first button enable all the buttons which u disabled at the first usingg button.setEnabled(true); 

button1.setOnClickListener(new onClickListener(){ 

public void onClick() 
{ 
button2.setEnabled(true); 
    button3.setEnabled(true); 
    button.setEnabled(true); 
} 
}; 
相關問題