如何使选项淡出但仍然可以在 Android Studio 上单击?

How to make an option fade but still be clickable on Android Studio?

我目前有两个复选框,单击其中一个时,另一个会自动取消选中。我想保留它,但未选中的变成褪色的白色,但如果用户决定改变 his/her 想法,它仍然可以点击和阅读。

这是我当前的代码:

 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);
        }
    });

当复选框处于选中和未选中状态时,您可以对复选框使用不同的图标。同时更改文本的颜色以使其看起来褪色。

为了帮助更改复选框图标,这很有用

Change icons of checked and unchecked for Checkbox for Android

您可以使用更改复选框图标和文本颜色, chk2.setTextColor(getResources().getColor(R.color.gray)); chk2.setButtonDrawable(R.drawable.unchecked);

chk2.setAlpha(0.5f) 会使它显得褪色。

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);
    }
});