簡體   English   中英

如何以編程方式更改復選框選中的顏色

[英]How to change checkbox checked color programmatically

我在 Android 中使用 CheckBox 視圖。 當它被選中時,我想改變它的顏色。 現在它是默認的深綠色,當它被選中時,我想把它改成不同的東西,當沒有選中時,只是默認顏色。

這是我的代碼:

CheckBox c = new CheckBox(this);
c.setId(View.generateViewId());

c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(buttonView.isChecked())
        {
            buttonView.setBackgroundColor(Color.rgb(64, 131, 207));
        }
        if(!buttonView.isChecked())
        {
            buttonView.setBackgroundColor(Color.WHITE);
        }

    }
});

問題是它並沒有改變正確的事情。 關於如何改變這種顏色的任何想法?

在此處輸入圖片說明

AppCompatCheckBox替換CheckBox並調用以下方法:

public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) {
    ColorStateList colorStateList = new ColorStateList(
            new int[][] {
                    new int[] { -android.R.attr.state_checked }, // unchecked
                    new int[] {  android.R.attr.state_checked }  // checked
            },
            new int[] {
                    uncheckedColor,
                    checkedColor
            }
    );
    checkBox.setSupportButtonTintList(colorStateList);
}

要為CompoundButton Tints着色,請嘗試使用API> 21及以下

if (Build.VERSION.SDK_INT < 21) {
    CompoundButtonCompat.setButtonTintList(button, ColorStateList.valueOf(tintColor));//Use android.support.v4.widget.CompoundButtonCompat when necessary else
} else {
    button.setButtonTintList(ColorStateList.valueOf(tintColor));//setButtonTintList is accessible directly on API>19
}

在 kotlin 中,你可以這樣寫:

refBtn.buttonTintList = ColorStateList.valueOf(Color.GRAY)

您可以通過所需的顏色更改更改 'Color.GRAY',或者編寫 Color.rgb() 並定義顏色:)

implement this file in res 

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" android:state_focused="true"
  android:drawable="@drawable/checkbox_on_background_focus_yellow" />
 <item android:state_checked="false" android:state_focused="true"
  android:drawable="@drawable/checkbox_off_background_focus_yellow" />
 <item android:state_checked="false"
  android:drawable="@drawable/checkbox_off_background" />
 <item android:state_checked="true"
  android:drawable="@drawable/checkbox_on_background" />
</selector>



and then add button to checkbox

<CheckBox android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:text="new checkbox"
 android:background="@drawable/checkbox_background" 
 android:button="@drawable/checkbox" />

這是 kotlin 擴展

fun AppCompatCheckBox.setTintAuto(enabledColor : Int, disabledColor : Int) {

val colorTint = ColorStateList(
    arrayOf(
        intArrayOf(-R.attr.state_checked),
        intArrayOf(R.attr.state_checked)
    ), intArrayOf(enabledColor , disabledColor )
)
this.buttonTintList = colorTint 
this.setTextColor(colorTint )
}

然后使用它

checkBox.setTintAuto(activeColor,disabledColor)

快樂編碼

您是否嘗試創建selector並將此selector selector分配給CheckBox ,例如:

//drawable file called cb_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/checked" />
    <item android:state_checked="false" android:drawable="@drawable/unchecked" />
</selector>

在布局文件中,將此文件應用於checkBox

<CheckBox
    android:id="@+id/myCheckBox"
    android:text="My CheckBox"
    android:button="@drawable/cb_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

@drawable/checked@drawable/unchecked是你的復選框的兩個圖像,所以你可以放一個你想要的顏色

或者不更改按鈕布局,將此屬性添加到復選框

android:buttonTint="@color/YOUR_CHECKMARK_COLOR_HERE"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM