【问题标题】:Android: Change the color of RadioButtons and checkboxes programmaticallyAndroid:以编程方式更改 RadioButtons 和复选框的颜色
【发布时间】:2015-12-05 00:29:27
【问题描述】:

我以编程方式在LinearLayout 中创建了RadioButtonCheckBox。但是,现在我想更改单选按钮的颜色和复选框的颜色。我用

RadioButton.setHighlightColor(Color.parseColor("#0c83bd"));

checkbox.setHighlightColor(Color.parseColor("#0c83bd"));

但它没有用。

【问题讨论】:

  • 使用背景图片
  • 但我想设置颜色不附加图片
  • android 支持库中是否有可用的功能?

标签: android checkbox colors radio-button


【解决方案1】:

使用 AppCompatCheckBoxAppCompatRadioButton 代替 CheckBoxRadioButton您的 xml 将具有:

<android.support.v7.widget.AppCompatCheckBox
    android:id="@+id/cbSelected"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/colorAccent" //This to set your default button color
    android:checked="true"/>

<android.support.v7.widget.AppCompatRadioButton
    android:id="@+id/rb"
    app:buttonTint="@color/colorAccent" //This to set your default button color
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

现在是 java 代码: 创建 ColorStateList

        ColorStateList colorStateList = new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_enabled} //enabled
                },
                new int[] {getResources().getColor(R.color.colorPrimary) }
        );

要以编程方式更改 AppCompatRadioButtonAppCompatCheckBox 的颜色,请调用 setSupportButtonTintList

AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
radioButton.setSupportButtonTintList(colorStateList);

AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected);
cbSelected.setSupportButtonTintList(colorStateList);

【讨论】:

    【解决方案2】:

    试试这个

    AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
    AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);
    

    插入

    RadioGroup newRadioButton = new RadioGroup(this);
    CheckBox newCheckBox = new CheckBox(this);
    

    【讨论】:

      【解决方案3】:

      您可以像这样创建动态RadioButton

      RadioButton male = new RadioButton(ReservationContact.this);
      male.setTextColor(Color.BLACK);
      male.setText("Male");
      male.setSelected(true);
      male.setId(0);
      

      这用于更改RadioButton的圆圈的默认颜色。

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          male.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(ReservationContact.this, R.color.background)));
      }
      male.setHighlightColor(getResources().getColor(R.color.background));
      

      【讨论】:

        【解决方案4】:

        对于CheckBox,将您的CheckBox 替换为AppCompatCheckBox 并调用以下方法:

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

        我认为RadioButton 应该是类似的。您可以检查android.R.attr中声明的属性并更改代码。

        【讨论】:

          【解决方案5】:

          我尝试了几种以编程方式实现它的方法,但除了遵循之外,没有一种方法对我有用。把它放在这里,以防有人觉得它有帮助。

          <androidx.appcompat.widget.AppCompatRadioButton
              android:id="@+id/radio_btn"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@null" />
          

          创建一个 ColorStateList

          private fun getRadioButtonColor(): ColorStateList {
              val states = arrayOf(
                      intArrayOf(-android.R.attr.state_checked),
                      intArrayOf(android.R.attr.state_checked))
          
              val colors = intArrayOf(
                      ContextCompat.getColor(context, R.color.grey2),
                      ContextCompat.getColor(context, R.color.actionBlue)
              )
          
              return ColorStateList(states, colors)
          }
          

          然后使用 CompoutButtonCompat 类中的 setButtonTintList 来设置这个 colorStateList

          CompoundButtonCompat.setButtonTintList(radio_btn, getRadioButtonColor())
          

          【讨论】:

            【解决方案6】:

            我继续ywwynm的回答。

            Google 限制了 setSupportButtonTintList,因此无法使用它。

            解决方法是将按钮用作 TintableCompoundButton 接口,其中方法不受限制。

            适用于 AppCompatRadioButton 的 API 19+。 AppCompatCheckbox 实现了相同的接口,因此理论上它应该也能正常工作,但我还没有测试过。

            玩得开心:)

            public static void setAppCompatRadioButtonColor(AppCompatRadioButton radioButton, 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
                        }
                );
                ((TintableCompoundButton) radioButton).setSupportButtonTintList(colorStateList);
            }
            

            【讨论】:

              【解决方案7】:

              尝试添加这一行:

              app:buttonTint="@color/yellow"
              

              xml 中的示例:

               <RadioButton
                    android:id="@+id/radio"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    app:buttonTint="@color/yellow"
                    android:text=" Radio"
                    android:textColor="@color/yellow" />
              

              【讨论】:

                【解决方案8】:
                   RadioButton rb=(RadioButton) findViewById(R.id.radioButton1);
                    rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub
                            rb.setButtonDrawable(R.drawable.image);
                            rb.setHighlightColor(Color.parseColor("#0c83bd"));
                
                
                        }
                    });
                  }
                

                【讨论】:

                • 如果用户设置了自己的 OnCheckedChangeListener 会怎样?
                猜你喜欢
                • 1970-01-01
                • 2015-04-24
                • 2016-03-24
                • 1970-01-01
                • 1970-01-01
                • 2015-04-02
                • 2011-07-31
                • 2017-06-04
                • 1970-01-01
                相关资源
                最近更新 更多