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

时间:2018-05-27 04:08:40

标签: android radio-button radio-group

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

这是我的活动代码:

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>

1 个答案:

答案 0 :(得分:0)

由于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" />

希望它有所帮助。