【问题标题】:How to disable the Onclick of RadioButton once it is clicked?单击后如何禁用 RadioButton 的 Onclick?
【发布时间】:2023-04-03 14:04:01
【问题描述】:

我正在创建一个测验应用程序,在其中提出问题并为用户提供选项。对于我使用 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);
}
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    点击单选按钮后试试这个:

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

    【讨论】:

      【解决方案2】:

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

      yourradiobutton.setClickable(false);
      

      【讨论】:

        【解决方案3】:

        如果我理解正确,您希望在单击某个 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() 不起作用,因此您应该尝试更改代码,以便 it loops through each RadioButton 在检查 radio0 时:

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

        【讨论】:

        • 仅供参考,如果您 setEnabled(false) 单选按钮,它将显示为灰色并且不会显示为“已选中”。它不仅禁用点击,而且禁用整个单选按钮(因此不再有“选中”状态)
        猜你喜欢
        • 1970-01-01
        • 2021-12-05
        • 2014-06-15
        • 2019-10-25
        • 2016-11-01
        • 2013-03-30
        • 2016-09-05
        • 2018-05-25
        • 1970-01-01
        相关资源
        最近更新 更多