【问题标题】:Change background for radio button programatically以编程方式更改单选按钮的背景
【发布时间】:2018-01-09 03:25:02
【问题描述】:

我有一个带有两个 RadioButtons 的 RadioGroup。我想在它们被禁用时以编程方式更改它们的颜色。

   <RadioGroup
        android:id="@+id/radio_group_transfer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginTop="4dp"
        android:gravity="center_vertical"
        android:layoutDirection="ltr"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/national_id_docs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/radio_btn_selector_left"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="@string/national_id"
            android:textColor="@drawable/radio_btn_textcolor_selector"
            style="@style/SimpleTextStyleSemiBold"/>

        <RadioButton
            android:id="@+id/passport"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/radio_btn_selector_right"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="@string/passport"
            android:textColor="@drawable/radio_btn_textcolor_selector"
            style="@style/SimpleTextStyleSemiBold"/>

    </RadioGroup>

我创建了新的灰色 XML 选择器,但是当我设置背景时它不起作用。

radio_btn_selector_left_disabled.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true">
        <shape>
            <solid android:color="@color/employee_non_editable_color_grey" />

            <stroke android:width="1dp" android:color="@color/employee_non_editable_color_grey" />

            <corners android:bottomLeftRadius="3dp" android:topLeftRadius="3dp"/>
        </shape>
    </item>
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />

            <stroke android:width="1dp" android:color="@color/employee_non_editable_color_grey" />

            <corners android:bottomLeftRadius="3dp" android:topLeftRadius="3dp"/>
        </shape>
    </item>
</selector>

我试过了

national_id_docs.setBackground(R.drawable.radio_btn_selector_left_disabled);

您能否建议将radio_btn_selector_left_disabled 用作单选按钮的最佳方式是什么?

【问题讨论】:

  • 试试this解决方案。
  • 我需要根据条件以编程方式进行。在 xml 中做不到
  • 使用一个包含android:state_checked="false"android:state_checked="true" 项目的xml 文件。仅您需要以编程方式在检查另一个时取消选中您的RadioButton。它将再次使用您的 xml。抱歉英语不好。

标签: android android-layout android-drawable android-radiobutton android-radiogroup


【解决方案1】:
    final RadioGroup rg = (RadioGroup) findViewById(R.id.radio_group_transfer);
    final RadioButton national = (RadioButton) findViewById(R.id.national_id_docs);
    final RadioButton passport = (RadioButton) findViewById(R.id.passport);

     rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (national.isChecked()) {
                national.setTextColor(Color.BLUE);
                national.setBackgroundColor(Color.BLUE);

                passport.setTextColor(Color.BLACK);
                passport.setBackgroundColor(Color.BLACK);
            }
            if (passport.isChecked()){
                passport.setTextColor(Color.BLUE);
                passport.setBackgroundColor(Color.BLUE);

                national.setTextColor(Color.BLACK);
                national.setBackgroundColor(Color.BLACK);
            }
        }
    });

【讨论】:

    【解决方案2】:

    获取您选择的单选按钮值

    final RadioGroup radioSpam = (RadioGroup) findViewById(R.id.radio_group_transfer);
                RadioButton radioButton;
                int selectedId = radioSpam.getCheckedRadioButtonId();
    

    解决方案 1: 改变colorAccent颜色

    < color name="colorAccent">#75aeff</color >
    

    解决方案 2: 在您的 style.xml 中创建您的样式并将其扩展到您的单选按钮。

    <style name="MyRadioButton" parent="Theme.AppCompat.Light">  
      <item name="colorControlNormal">@color/indigo</item>
      <item name="colorControlActivated">@color/pink</item>
    </style>  
    
    <RadioButton  
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Radio Button"
        android:theme="@style/MyRadioButton"/>
    

    解决方案 3: 使用 AppCompatRadioButton

     <android.support.v7.widget.AppCompatRadioButton
            android:id="@+id/rbtn_test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:buttonTint="@color/primary" />
    

    【讨论】:

      猜你喜欢
      • 2011-03-07
      • 2020-05-15
      • 2016-11-06
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多