在 Android Studio 上选中复选框 1 时如何取消选中复选框,反之亦然?
How to make checkbox2 uncheck when checking checkbox1 and vice versa on AndroidStudio?
如何让复选框取消选中之前选中的复选框,反之亦然。我正在尝试开发一个应用程序,在其中检查您只能选择两个选项之一。我似乎无法让 onCheckedChangeListener 工作。我不想要单选按钮,因为它们无法取消。
我尝试了 Noob 在类似问题下发布的这段代码。
I need to uncheck a checkbox when a particular checkbox is checked
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setalarm);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final CheckBox chk1 = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox chk2 = (CheckBox) findViewById(R.id.checkBox2);
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chk2.setChecked(false);
}
});
chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chk1.setChecked(false);
}
});
}
如果您确定不想使用 RadioButton
并且只有两个可能的 CheckBox
选项,那么像这样简单的事情应该可以解决问题:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_name);
final CheckBox c1 = (CheckBox) findViewById(R.id.c1);
final CheckBox c2 = (CheckBox) findViewById(R.id.c2);
c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
c2.setChecked(false);
c1.setChecked(b);
}
});
c2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
c1.setChecked(false);
c2.setChecked(b);
}
});
}
如何让复选框取消选中之前选中的复选框,反之亦然。我正在尝试开发一个应用程序,在其中检查您只能选择两个选项之一。我似乎无法让 onCheckedChangeListener 工作。我不想要单选按钮,因为它们无法取消。
我尝试了 Noob 在类似问题下发布的这段代码。 I need to uncheck a checkbox when a particular checkbox is checked
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setalarm);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final CheckBox chk1 = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox chk2 = (CheckBox) findViewById(R.id.checkBox2);
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chk2.setChecked(false);
}
});
chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chk1.setChecked(false);
}
});
}
如果您确定不想使用 RadioButton
并且只有两个可能的 CheckBox
选项,那么像这样简单的事情应该可以解决问题:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_name);
final CheckBox c1 = (CheckBox) findViewById(R.id.c1);
final CheckBox c2 = (CheckBox) findViewById(R.id.c2);
c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
c2.setChecked(false);
c1.setChecked(b);
}
});
c2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
c1.setChecked(false);
c2.setChecked(b);
}
});
}