如何使只有一个单选按钮可点击?

How to make only one radio button clickable?

我想做一个测验应用程序。一个问题有 7 个单选按钮。在此之前,我对每个单选按钮使用 onClick 方法。问题是用户可以在一个问题中单击多个单选按钮(我将单选按钮放在单选组中)。现在我想使用 RadioGroup onCheckedChangeListener 但问题仍然存在。如何解决?

这是我的 activity 代码:

public class MainActivity extends AppCompatActivity {

int score = 0;

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

    final RadioGroup question1_radioGroup = (RadioGroup) findViewById(R.id.question1_radiogroup);
    question1_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            View radioButton = question1_radioGroup.findViewById(checkedId);
            boolean checked = ((RadioButton) radioButton).isChecked();
            switch(radioButton.getId()) {
                case R.id.question1_radioButton1:
                    if(checked) {score = score + 1;}
                    break;
                case R.id.question1_radioButton2:
                    if(checked) {score = score + 2;}
                    break;
                case R.id.question1_radioButton3:
                    if(checked) {score = score + 3;}
                    break;
                case R.id.question1_radioButton4:
                    if(checked) {score = score + 4;}
                    break;
                case R.id.question1_radioButton5:
                    if(checked) {score = score + 5;}
                    break;
                case R.id.question1_radioButton6:
                    if(checked) {score = score + 6;}
                    break;
                case R.id.question1_radioButton7:
                    if(checked) {score = score + 7;}
                    break;
            }

        }
    });

    final RadioGroup question2_radioGroup = (RadioGroup) findViewById(R.id.question2_radiogroup);
    question2_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            View radioButton = question1_radioGroup.findViewById(checkedId);
            boolean checked = ((RadioButton) radioButton).isChecked();
            switch(radioButton.getId()) {
                case R.id.question2_radioButton1:
                    if(checked) {score = score + 1;}
                    break;
                case R.id.question2_radioButton2:
                    if(checked) {score = score + 2;}
                    break;
                case R.id.question2_radioButton3:
                    if(checked) {score = score + 3;}
                    break;
                case R.id.question2_radioButton4:
                    if(checked) {score = score + 4;}
                    break;
                case R.id.question2_radioButton5:
                    if(checked) {score = score + 5;}
                    break;
                case R.id.question2_radioButton6:
                    if(checked) {score = score + 6;}
                    break;
                case R.id.question2_radioButton7:
                    if(checked) {score = score + 7;}
                    break;
            }
        }
    });

    final RadioGroup question3_radioGroup = (RadioGroup) findViewById(R.id.question3_radiogroup);
    question3_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            View radioButton = question1_radioGroup.findViewById(checkedId);
            boolean checked = ((RadioButton) radioButton).isChecked();
            switch(radioButton.getId()) {
                case R.id.question3_radioButton1:
                    if(checked) {score = score + 1;}
                    break;
                case R.id.question3_radioButton2:
                    if(checked) {score = score + 2;}
                    break;
                case R.id.question3_radioButton3:
                    if(checked) {score = score + 3;}
                    break;
                case R.id.question3_radioButton4:
                    if(checked) {score = score + 4;}
                    break;
                case R.id.question3_radioButton5:
                    if(checked) {score = score + 5;}
                    break;
                case R.id.question3_radioButton6:
                    if(checked) {score = score + 6;}
                    break;
                case R.id.question3_radioButton7:
                    if(checked) {score = score + 7;}
                    break;
            }
        }
    });

    final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
    generate_result_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
            generate_result_intent.putExtra("score", score);
            startActivity(generate_result_intent);
        }
    });
}


}

这是我的 xml 文件:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/instruction_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/instruction" />

// Question 1
<TextView
    android:id="@+id/question1_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question1" />

<RadioGroup
    android:id="@+id/question1_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question1_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <RadioButton
            android:id="@+id/question1_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <RadioButton
            android:id="@+id/question1_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />

        <RadioButton
            android:id="@+id/question1_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />

        <RadioButton
            android:id="@+id/question1_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />

        <RadioButton
            android:id="@+id/question1_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />

        <RadioButton
            android:id="@+id/question1_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 2
<TextView
    android:id="@+id/question2_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question2" />

<RadioGroup
    android:id="@+id/question2_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question2_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <RadioButton
            android:id="@+id/question2_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <RadioButton
            android:id="@+id/question2_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />

        <RadioButton
            android:id="@+id/question2_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />

        <RadioButton
            android:id="@+id/question2_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />

        <RadioButton
            android:id="@+id/question2_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />

        <RadioButton
            android:id="@+id/question2_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 3
<TextView
    android:id="@+id/question3_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question3" />

<RadioGroup
    android:id="@+id/question3_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question3_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <RadioButton
            android:id="@+id/question3_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <RadioButton
            android:id="@+id/question3_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />

        <RadioButton
            android:id="@+id/question3_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />

        <RadioButton
            android:id="@+id/question3_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />

        <RadioButton
            android:id="@+id/question3_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />

        <RadioButton
            android:id="@+id/question3_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

<Button
    android:id="@+id/jana_keputusan_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Jana Keputusan" />

</LinearLayout>

由于 RadioGroup 中的 LinearLayout,它无法正常工作。由于它们之间存在 LinearLayout,因此所有 RadioButton 未组合在一起。

RadioButton 应该是RadioGroup 的直接子节点,否则分组不起作用。

只需像这样更改您的代码即可:

<RadioGroup
android:id="@+id/question1_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation='horizontal'>


    <RadioButton
        android:id="@+id/question1_radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1" />

    <RadioButton
        android:id="@+id/question1_radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2" />

    <RadioButton
        android:id="@+id/question1_radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="3" />

    <RadioButton
        android:id="@+id/question1_radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="4" />

    <RadioButton
        android:id="@+id/question1_radioButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="5" />

    <RadioButton
        android:id="@+id/question1_radioButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="6" />

    <RadioButton
        android:id="@+id/question1_radioButton7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="7" />

希望对您有所帮助。