如何取消选中 radiogroup 中的单选按钮并收听操作
How to uncheck a radio button in radiogroup and listen to the action
我有 3 个自定义单选按钮,默认情况下未选中它们。它们就像一个过滤器。我想知道是否有办法取消选中已选中的按钮并收听该操作。所以基本上我可以取消过滤器。
我知道在这种情况下我应该使用 checkbox
,但我不想检查超过一个!!
RadioGroup radioGroup;
RadioButton radioYellow;
RadioButton radioGreen;
RadioButton radioRed;
boolean isChecked;
void handleRadioButtons(){
radioRed.setChecked(false);
radioGreen.setChecked(false);
radioYellow.setChecked(false);
showAllCards();
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// RadioButton rad = group.findViewById(checkedId);
Log.i("isChecked: ", isChecked + "");
//I want to call showAllCards() when no button is checked
switch (checkedId) {
case R.id.radioRed:
showRedCards();
break;
case R.id.radioGreen:
showGreenCards();
break;
case R.id.radioYellow:
showYellowCards();
break;
}
}
});
}
试试这个代码,希望对你有帮助..
RadioGroup radioGroup;
RadioButton radioYellow;
RadioButton radioGreen;
RadioButton radioRed;
boolean isChecked;
void handleRadioButtons(){
radioRed.setChecked(false);
radioGreen.setChecked(false);
radioYellow.setChecked(false);
showAllCards();
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// RadioButton rad = group.findViewById(checkedId);
Log.i("isChecked: ", isChecked + "");
//I want to call showAllCards() when no button is checked
switch (checkedId) {
case R.id.radioRed:
// check if it is checked or not...
if(radioRed.isChecked())
radioRed.setChecked(false);
else
radioRed.setChecked(true);
// showRedCards();
break;
case R.id.radioGreen:
if(radioGreen.isChecked())
radioGreen.setChecked(false);
else
radioGreen.setChecked(true);
//showGreenCards();
break;
case R.id.radioYellow:
if(radioYellow.isChecked())
radioYellow.setChecked(false);
else
radioYellow.setChecked(true);
//showYellowCards();
break;
}
}
});
}
单选按钮似乎不起作用,所以我将它们全部替换为复选框并手动处理。它奏效了!
greenCheckBox.setChecked(false);
yellowCheckBox.setChecked(false);
redCheckBox.setChecked(false);
showAllCards();
greenCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked){
yellowCheckBox.setChecked(false);
redCheckBox.setChecked(false);
showGreenCards();
}else {
showAllCards();
}
}
});
yellowCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked){
greenCheckBox.setChecked(false);
redCheckBox.setChecked(false);
showYellowCards();
}else {
showAllCards();
}
}
});
redCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked){
yellowCheckBox.setChecked(false);
greenCheckBox.setChecked(false);
showRedCards();
}else {
showAllCards();
}
}
});
你应该有一个 hideRedCards()
和 hideBlueCard()
,hideGreenCards()
方法来隐藏你的卡片,当 radioButton 未选中时,调用此方法
void handleRadioButtons() {
radioRed.setChecked(false);
radioGreen.setChecked(false);
radioYellow.setChecked(false);
// showAllCards();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// RadioButton rad = group.findViewById(checkedId);
Log.i("isChecked: ", isChecked + "");
if (radioRed.isChecked()) {
radioRed.setChecked(true);
showRedCards();
} else{
radioRed.setChecked(false);
HideRedCards();
}
if (radioGreen.isChecked()) {
radioGreen.setChecked(true);
showGreenCards();
} else{
radioGreen.setChecked(false);
HideGreenCards();
}
if (radioYellow.isChecked()) {
radioYellow.setChecked(true);
showYellowCards();
} else{
radioYellow.setChecked(false);
HideYellowCards();
}
}
});
}
我有 3 个自定义单选按钮,默认情况下未选中它们。它们就像一个过滤器。我想知道是否有办法取消选中已选中的按钮并收听该操作。所以基本上我可以取消过滤器。
我知道在这种情况下我应该使用 checkbox
,但我不想检查超过一个!!
RadioGroup radioGroup;
RadioButton radioYellow;
RadioButton radioGreen;
RadioButton radioRed;
boolean isChecked;
void handleRadioButtons(){
radioRed.setChecked(false);
radioGreen.setChecked(false);
radioYellow.setChecked(false);
showAllCards();
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// RadioButton rad = group.findViewById(checkedId);
Log.i("isChecked: ", isChecked + "");
//I want to call showAllCards() when no button is checked
switch (checkedId) {
case R.id.radioRed:
showRedCards();
break;
case R.id.radioGreen:
showGreenCards();
break;
case R.id.radioYellow:
showYellowCards();
break;
}
}
});
}
试试这个代码,希望对你有帮助..
RadioGroup radioGroup;
RadioButton radioYellow;
RadioButton radioGreen;
RadioButton radioRed;
boolean isChecked;
void handleRadioButtons(){
radioRed.setChecked(false);
radioGreen.setChecked(false);
radioYellow.setChecked(false);
showAllCards();
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// RadioButton rad = group.findViewById(checkedId);
Log.i("isChecked: ", isChecked + "");
//I want to call showAllCards() when no button is checked
switch (checkedId) {
case R.id.radioRed:
// check if it is checked or not...
if(radioRed.isChecked())
radioRed.setChecked(false);
else
radioRed.setChecked(true);
// showRedCards();
break;
case R.id.radioGreen:
if(radioGreen.isChecked())
radioGreen.setChecked(false);
else
radioGreen.setChecked(true);
//showGreenCards();
break;
case R.id.radioYellow:
if(radioYellow.isChecked())
radioYellow.setChecked(false);
else
radioYellow.setChecked(true);
//showYellowCards();
break;
}
}
});
}
单选按钮似乎不起作用,所以我将它们全部替换为复选框并手动处理。它奏效了!
greenCheckBox.setChecked(false);
yellowCheckBox.setChecked(false);
redCheckBox.setChecked(false);
showAllCards();
greenCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked){
yellowCheckBox.setChecked(false);
redCheckBox.setChecked(false);
showGreenCards();
}else {
showAllCards();
}
}
});
yellowCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked){
greenCheckBox.setChecked(false);
redCheckBox.setChecked(false);
showYellowCards();
}else {
showAllCards();
}
}
});
redCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked){
yellowCheckBox.setChecked(false);
greenCheckBox.setChecked(false);
showRedCards();
}else {
showAllCards();
}
}
});
你应该有一个 hideRedCards()
和 hideBlueCard()
,hideGreenCards()
方法来隐藏你的卡片,当 radioButton 未选中时,调用此方法
void handleRadioButtons() {
radioRed.setChecked(false);
radioGreen.setChecked(false);
radioYellow.setChecked(false);
// showAllCards();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// RadioButton rad = group.findViewById(checkedId);
Log.i("isChecked: ", isChecked + "");
if (radioRed.isChecked()) {
radioRed.setChecked(true);
showRedCards();
} else{
radioRed.setChecked(false);
HideRedCards();
}
if (radioGreen.isChecked()) {
radioGreen.setChecked(true);
showGreenCards();
} else{
radioGreen.setChecked(false);
HideGreenCards();
}
if (radioYellow.isChecked()) {
radioYellow.setChecked(true);
showYellowCards();
} else{
radioYellow.setChecked(false);
HideYellowCards();
}
}
});
}