2017-08-28 33 views
-4

我正在創建一個測驗應用程序,其中提出了一個問題,並給了用戶選項。對於我已經使用RadioButton的選項,所以我想要的是,一旦用戶點擊任何一個選項,它不應該允許點擊任何其他選項但問題是,它仍然可以點擊。如果可能請寫代碼。謝謝!如何禁用RadioButton單擊後點擊Onclick?

private int Question_no=0; 
private Boolean Boolean_Var=false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_question1); 

    String[] Question_Array = getResources().getStringArray(R.array.Question1); 
    TextView Questions = (TextView) findViewById(R.id.Question); 
    Questions.setText(Question_Array[Question_no]); 

    String[] Radio_Button1_Array = getResources().getStringArray(R.array.Option_1); 
    RadioButton Radio_Button1 = (RadioButton) findViewById(R.id.radioButton1); 
    Radio_Button1.setText(Radio_Button1_Array[Question_no]); 

    String[] Radio_Button2_Array = getResources().getStringArray(R.array.Option_2); 
    RadioButton Radio_Button2 = (RadioButton) findViewById(R.id.radioButton2); 
    Radio_Button2.setText(Radio_Button2_Array[Question_no]); 

    findViewById(R.id.MenuButton).setVisibility(View.INVISIBLE); 
    findViewById(R.id.NextButton).setVisibility(View.INVISIBLE); 
} 

public void On_RadioButton1_Click (View view) 
{ 
    if (Boolean_Var == false) { 
     String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1); 
     String CorrectAns = CorrectAns_Array[Question_no]; 
     String[] Answer_Array = getResources().getStringArray(R.array.Option_1); 
     String Answer = Answer_Array[Question_no]; 


     if (Answer.equals(CorrectAns)) { 
      RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton1); 
      Right_Ans.setTextColor(Color.GREEN); 
      AnswerSubmitted(); 
     } else { 
      RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton1); 
      Wrong_Ans.setTextColor(Color.RED);; 
      GreenTick(); 
      AnswerSubmitted(); 
     } 
    } 
    Boolean_Var = true; 

} 

public void On_RadioButton2_Click(View view) 
{ 
    if(Boolean_Var==false) 
    { 
     String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1); 
     String CorrectAns = CorrectAns_Array[Question_no]; 
     String[] Answer_Array = getResources().getStringArray(R.array.Option_2); 
     String Answer = Answer_Array[Question_no]; 


     if(Answer.equals(CorrectAns)) 
     { 
      RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton2); 
      Right_Ans.setTextColor(Color.GREEN); 
     } 
     else 
     { 
      RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton2); 
      Wrong_Ans.setTextColor(Color.RED); 
      GreenTick(); 
     } 
    } 
    Boolean_Var=true; 
    AnswerSubmitted(); 
} 

public void GreenTick() 
{ 
    String[] Answer1_Array = getResources().getStringArray(R.array.Option_1); 
    String Answer1 = Answer1_Array[Question_no]; 

    String[] Answer2_Array = getResources().getStringArray(R.array.Option_2); 
    String Answer2 = Answer2_Array[Question_no]; 

    String[] Answer3_Array = getResources().getStringArray(R.array.Option_3); 
    String Answer3 = Answer3_Array[Question_no]; 

    String[] The_CorrectAns_Array = getResources().getStringArray(R.array.Answer1); 
    String The_CorrectAnsIs = The_CorrectAns_Array[Question_no]; 

    if(The_CorrectAnsIs.equals(Answer1)) 
    { 
     Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton1); 
     Option_with_CorrectAns.setTextColor(Color.GREEN); 
    } 
    else if(The_CorrectAnsIs.equals(Answer2)) 
    { 
     Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton2); 
     Option_with_CorrectAns.setTextColor(Color.GREEN); 
    } 
    else if(The_CorrectAnsIs.equals(Answer3)) 
    { 
     Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton3); 
     Option_with_CorrectAns.setTextColor(Color.GREEN); 
    } 
    else 
    { 
     Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton4); 
     Option_with_CorrectAns.setTextColor(Color.GREEN); 
    } 
} 

public void AnswerSubmitted() 
{ 
    findViewById(R.id.MenuButton).setVisibility(View.VISIBLE); 
    findViewById(R.id.NextButton).setVisibility(View.VISIBLE); 
} 
} 

回答

0

如果我的理解正確,您希望在單擊某個RadioButton後禁用RadioGroup。只需添加一個OnCheckedChangedListener,並且如果單擊的RadioButton的ID是正確的,則使用setEnabled(false)禁用該組。當然,除非您的代碼中還有其他邏輯可以重新啓用,否則無法重新啓用RadioGroup。

RadioGroup radioGroup = (RadioGroup)findViewById (R.id.radioGroup1); 
radioGroup.setOnCheckedChangedListener (new OnCheckedChangedListener(){ 
public void onCheckedChanged(RadioGroup group, int checkedId) 
{ 
if (checkedId == R.id.radio0) 
group.setEnabled(false); 
} 
}); 

編輯:看來的setEnabled()不工作,那麼你應該嘗試在檢查radio0改變你的代碼,以便it loops through each單選:

if (checkedId == R.id.radio0){ 
for(int i = 0; i < group.getChildCount(); i++){ 
     ((RadioButton)rg1.getChildAt(i)).setEnabled(false); 
    } 
} 
0

您可以使用setClickable(false);。爲此:

yourradiobutton.setClickable(false); 
0

試試這個後點擊單選:

yourradiobutton.setClickable(false); 
yourradiobutton.setClickable(false);