Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在单击单选按钮后禁用单选按钮的Onclick?_Java_Android - Fatal编程技术网

Java 如何在单击单选按钮后禁用单选按钮的Onclick?

Java 如何在单击单选按钮后禁用单选按钮的Onclick?,java,android,Java,Android,我正在创建一个测验应用程序,其中有一个问题,并为用户提供选项。对于选项,我使用了RadioButton,因此我想要的是,一旦用户单击任何一个选项,它不应允许单击任何其他选项,但问题是它仍然可以单击。如果可能,请编写代码。谢谢 private int Question_no=0; private Boolean Boolean_Var=false; @Override protected void onCreate(Bundle savedInstanceState) { super.o

我正在创建一个测验应用程序,其中有一个问题,并为用户提供选项。对于选项,我使用了RadioButton,因此我想要的是,一旦用户单击任何一个选项,它不应允许单击任何其他选项,但问题是它仍然可以单击。如果可能,请编写代码。谢谢

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

如果我理解正确,您希望在单击某个单选按钮后禁用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时使用RadioButton:

if (checkedId == R.id.radio0){
 for(int i = 0; i < group.getChildCount(); i++){
        ((RadioButton)rg1.getChildAt(i)).setEnabled(false);
    }
}
if(checkedId==R.id.0){
对于(int i=0;i
您可以使用
setClickable(false)。为此:

yourradiobutton.setClickable(false);

单击radiobutton后尝试此操作:

   yourradiobutton.setClickable(false);
yourradiobutton.setClickable(false);
仅供参考,如果您将单选按钮设置为Enabled(false),它将变灰,并且不会显示为“checked”。它不仅禁用单击,而且禁用整个单选按钮(因此不再显示“选中”状态)