我尝试过多组复选框,其中只有一个可以被检查但是在尝试将此功能实现到第二组复选框时,当我尝试转到包含这些复选框组的活动时,应用程序崩溃。 auton组的复选框正在使用只有一个能够从5中检出但是因为我试图用port_添加相同的东西我没有运气。
auton_nothing = (CheckBox)findViewById(R.id.nothing_cb);
auton_touch= (CheckBox)findViewById(R.id.touchdefense_cb);
auton_cross= (CheckBox)findViewById(R.id.cross_cb);
auton_high= (CheckBox)findViewById(R.id.hg_cb);
auton_low= (CheckBox)findViewById(R.id.lg_cb);
auton_nothing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
auton_nothing.setChecked(b);
auton_touch.setChecked(false);
auton_cross.setChecked(false);
auton_high.setChecked(false);
auton_low.setChecked(false);
}
});
auton_touch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
auton_nothing.setChecked(false);
auton_touch.setChecked(b);
auton_cross.setChecked(false);
auton_high.setChecked(false);
auton_low.setChecked(false);
}
});
auton_cross.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
auton_nothing.setChecked(false);
auton_touch.setChecked(false);
auton_cross.setChecked(b);
auton_high.setChecked(false);
auton_low.setChecked(false);
}
});
auton_high.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
auton_nothing.setChecked(false);
auton_touch.setChecked(false);
auton_cross.setChecked(false);
auton_high.setChecked(b);
auton_low.setChecked(false);
}
});
auton_low.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
auton_nothing.setChecked(false);
auton_touch.setChecked(false);
auton_cross.setChecked(false);
auton_high.setChecked(false);
auton_low.setChecked(b);
}
});
port_na= (CheckBox)findViewById(R.id.portc_na_cb);
port_na.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
port_na.setChecked(b);
port_attempt.setChecked(false);
port_diff.setChecked(false);
port_easy.setChecked(false);
}
});
port_attempt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
port_na.setChecked(false);
port_attempt.setChecked(b);
port_diff.setChecked(false);
port_easy.setChecked(false);
}
});
port_diff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
port_na.setChecked(false);
port_attempt.setChecked(false);
port_diff.setChecked(b);
port_easy.setChecked(false);
}
});
port_easy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
port_na.setChecked(false);
port_attempt.setChecked(false);
port_diff.setChecked(false);
port_easy.setChecked(b);
}
});
port_attempt= (CheckBox)findViewById(R.id.portc_attempted_cb);
port_diff= (CheckBox)findViewById(R.id.portc_difficult_cb);
port_easy= (CheckBox)findViewById(R.id.portc_easy_cb);
答案 0 :(得分:0)
试试这个........
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup"
android:checkedButton="1"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/check_a"
android:id="@+id/checkBox_first"
android:onClick="onCheckboxClicked" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/check_b"
android:id="@+id/checkBox_second"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/check_c"
android:id="@+id/checkBox_third"
android:onClick="onCheckboxClicked"/>
</RadioGroup>
</LinearLayout>
MainActivity.java文件
public class MainActivity extends ActionBarActivity {
private CheckBox checkBox1, checkBox2, checkBox3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1 = (CheckBox) findViewById(R.id.checkBox_first);
checkBox2 = (CheckBox) findViewById(R.id.checkBox_second);
checkBox3 = (CheckBox) findViewById(R.id.checkBox_third);
}
public void onCheckboxClicked(View view) {
switch(view.getId()) {
case R.id.checkBox1:
checkBox2.setChecked(false);
checkBox3.setChecked(false);
break;
case R.id.checkBox2:
checkBox3.setChecked(false);
checkBox1.setChecked(false);
break;
case R.id.checkBox3:
checkBox1.setChecked(false);
checkBox2.setChecked(false);
break;
}
}