简体   繁体   English

单击和取消单击时如何更改复选框颜色?

[英]How can I change Checkbox color when I click and unclick?

When I click on checkbox color change of that checkbox and rest of check box remain another color so how can I change the color of checkbox?当我单击该复选框的复选框颜色更改并且复选框的 rest 保持另一种颜色时,如何更改复选框的颜色?

if (CountryList.get(pos).getSelected()) {
    CountryList.get(pos).setSelected(false);
    holder.country.setBackgroundResource(R.color.btnbckgrd);
} else {
    CountryList.get(pos).setSelected(true);
    holder.country.setBackgroundResource(R.color.lightgreen);
}
try this:
mSelectedItem = -1;
holder.country.setBackgroundResource(mSelectedItem == position ? 
R.color.lightgreen: R.color.btnbckgrd);

you can use style for this您可以为此使用样式

<style name="yourStyle" parent="Base.Theme.AppCompat">
    <item name="colorAccent">your_color</item> <!-- for uncheck state -->
    <item name="android:textColorSecondary">your color</item> <!-- for check state -->
</style>

set it in xml like this像这样在 xml 中设置它

<CheckBox
     android:theme="@style/yourStyle"
/>

Have you tried android:tint="@color/your_color" in your layout file??您是否在布局文件中尝试过android:tint="@color/your_color"

  • I think this might help you.我想这可能会对你有所帮助。

     if(checkbox.isChecked()){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { checkbox.setButtonTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.yourcheckcolor))); } }else{ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { checkbox.setButtonTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.youruncheckcolor))); } }

Correct answer which is working on all API would be:适用于所有 API 的正确答案是:

Create style:创建风格:

<style name="CheckBox">
        <item name="colorControlNormal">@color/light_grey</item>
        <item name="colorControlActivated">@color/ts_white</item>
    </style>

and use it like this:并像这样使用它:

   <androidx.appcompat.widget.AppCompatCheckBox
                  .....
                    android:theme="@style/Bonus.CheckBox"
                  ..... />

after setting style you wont going to need to check views background programmaticaly设置样式后,您将不需要以编程方式检查视图背景

try this:尝试这个:

  final CheckBox cbox= (CheckBox) findViewById(R.id.cb_flash);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Set the Flash CheckBox Text Color
            cbox.setTextColor(Color.BLUE);

            // Set the Flash CheckBox Background Color
            cbox.setBackgroundColor(Color.parseColor("#cbff75"));
        }
    });

 if(checkbox.isChecked()){
   cbox.setTextColor(Color.BLUE);

            // Set the Flash CheckBox Background Color
            cbox.setBackgroundColor(Color.parseColor("#cbff75"));

  }else{
 //chenge color
   }

refer this link another method 请参考此链接另一种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅在单击时如何更改颜色 - How to change the color only when I click 我在适配器中创建了复选框和按钮动态,当我单击复选框时,我需要更改按钮中的文本颜色。 但我无法刷新视图 - I created checkbox and button dynamic in adapter, when i click checkbox i need to change Text Color in button. But i can't refresh the view 取消单击按钮或视图时如何添加 function? - How to add a function when I unclick a button or a view? 如何更改微调器复选框的大小或颜色? - How can I change the size or the color of my spinner checkbox? 如何更改 Android 上的 CheckBox 焦点颜色? - How can I change the CheckBox focus color on Android? 当我点击tablayout的标签时,如何更改波浪效果的颜色? - How can i change the color of wave effect when I click on a tab of tablayout? 安卓 如何在点击时更改ListView文本颜色? - Android. How can I change a ListView text color on click? 如何在单击时更改 RecyclerView 上未选中和选中项目的颜色? - How can I change the color of the unselected and selected items on a RecyclerView on click? 单击imageview时如何更改gridview - How can I change gridview when I click imageview 如何从调色板android更改按钮单击时我的画笔的颜色 - how can i change the color of my paint brush on button click from the color palette android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM