无法处理单选按钮选择

时间:2017-12-01 11:20:39

标签: android radio-button radio-group

我在一个广播组中创建了一个带有四个单选按钮的MCQ应用程序。问题是我想选择一个单选按钮并在那时禁用其他三个单选按钮选择。只有一个单选按钮选择和禁用其他单选按钮选择。 请帮助我:我将非常感谢你们所有人。

----------

Xml File

 <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/radioGroup1">
//These are four radio buttons from which i have to select one radio button and selection of other three radio buttons must be disabled.
        <RadioButton
            android:id="@+id/choice1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:background="@android:color/darker_gray"
            android:gravity="left|center_vertical"
            android:onClick="onClick"
            android:padding="4dp"
            android:text="A"
            android:textColor="#000000"
            android:textSize="16dp" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="B"
        android:onClick="onClick"
        android:background="@android:color/darker_gray"
        android:textColor="#000000"
        android:padding="4dp"
        android:textSize="16dp"
        android:gravity="left|center_vertical"
        android:layout_marginBottom="5dp"
        android:id="@+id/choice2"/>

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="C"
        android:onClick="onClick"
        android:background="@android:color/darker_gray"
        android:textColor="#000000"
        android:padding="4dp"
        android:textSize="16dp"
        android:gravity="left|center_vertical"
        android:layout_marginBottom="5dp"
        android:id="@+id/choice3"/>

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="D"
        android:onClick="onClick"
        android:background="@android:color/darker_gray"
        android:textColor="#000000"
        android:padding="4dp"
        android:textSize="16dp"
        android:gravity="left|center_vertical"
        android:layout_marginBottom="5dp"
        android:id="@+id/choice4" />
</RadioGroup>

----------

Java File:
 mQuestionView = (TextView) findViewById(R.id.question);
        mButtonChoice1 = (RadioButton) findViewById(R.id.choice1);
        mButtonChoice2 = (RadioButton) findViewById(R.id.choice2);
        mButtonChoice3 = (RadioButton) findViewById(R.id.choice3);
        mButtonChoice4 = (RadioButton) findViewById(R.id.choice4);
 }
  //Java file with only onclick button code:
 public void onClick(View view) {
            Button answer = (Button) view;
            // Is the button now checked?
            //here must be the code of my problem
        }

2 个答案:

答案 0 :(得分:0)

对于禁用RadioGroup,请尝试下面的代码。如果它不起作用,请尝试循环。

hello?: string, moo?: boolean

如果RadioGroup radioGroup = (RadioGroup)findViewById (R.id.radioGroup1); radioGroup.setOnCheckedChangedListener (new OnCheckedChangedListener(){ public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.choice1) group.setEnabled(false); }else if(/*other conditions here*/) }); 不起作用,那么您应该在检查setEnabled()时尝试更改代码loops through each RadioButton

radio0

答案 1 :(得分:0)

我不太确定你要求的是什么,但我认为这是一个解决方案:

public void onClick(View view)
{
    ArrayList<Integer> ids = new ArrayList<Integer>();
    ids.add(mButtonChoice1.getId());
    ids.add(mButtonChoice2.getId());
    ids.add(mButtonChoice3.getId());
    ids.add(mButtonChoice4.getId());
    for(Integer i : ids)
    {
        if(i!= view.getId())
            ((RadioButton) findViewById(i)).setEnabled(false);
    }
}