问题
I currently have a two checkboxes, where when one is clicked, the other automatically unchecks. I would like to keep that however have the unchecked one become a faded white color, yet still be clickable and readable if the user decides to change his/her mind.
This is the current code I have:
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (CheckBox.isChecked(chk1)) {
chk2.setChecked(false);
chk1.setChecked(b);
chk2.setAlpha(0.5f);
}
});
chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (CheckBox.isChecked(chk2)) {
chk1.setChecked(false);
chk2.setChecked(b);
chk1.setAlpha(0.5f);
}
});
回答1:
chk2.setAlpha(0.5f)
would make it appear faded.
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chk2.setChecked(false);
chk1.setChecked(b);
chk2.setAlpha(0.5f);
chk1.setAlpha(1f);
}
});
chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chk1.setChecked(false);
chk2.setChecked(b);
chk1.setAlpha(0.5f);
chk2.setAlpha(1f);
}
});
回答2:
You can use different icons for checkboxes when they are in selected and unselected state. Also change the color of the text to make it look faded.
To help with changing checkbox icons, this is helpful
Change icons of checked and unchecked for Checkbox for Android
You can use to change the checkbox icon and text color, chk2.setTextColor(getResources().getColor(R.color.gray)); chk2.setButtonDrawable(R.drawable.unchecked);
来源:https://stackoverflow.com/questions/28102745/how-to-make-an-option-fade-but-still-be-clickable-on-android-studio