Android 如何使只有一个单选按钮可点击?
我想做一个测验应用程序。一个问题有7个单选按钮。在此之前,我对每个单选按钮都使用onClick方法。问题是用户可以在一个问题中单击多个单选按钮(我将单选按钮放在单选组中)。现在我想使用RadioGroup onCheckedChangeListener,但问题仍然是一样的。如何解决这个问题 这是我的活动代码:Android 如何使只有一个单选按钮可点击?,android,radio-button,radio-group,Android,Radio Button,Radio Group,我想做一个测验应用程序。一个问题有7个单选按钮。在此之前,我对每个单选按钮都使用onClick方法。问题是用户可以在一个问题中单击多个单选按钮(我将单选按钮放在单选组中)。现在我想使用RadioGroup onCheckedChangeListener,但问题仍然是一样的。如何解决这个问题 这是我的活动代码: public class MainActivity extends AppCompatActivity { int score = 0; @Override protected voi
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
//问题2
//问题3
由于RadioGroup内部的线性布局,它无法工作。由于单选按钮之间存在线性布局,因此未将所有单选按钮分组在一起
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" />
希望有帮助。你能为radio group共享你的xml吗?我已经把它放好了。@RuzainIrfan删除radio group中的线性布局,这样做很好。