以编程方式更改单选按钮的背景

时间:2018-01-08 17:39:50

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

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

   <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用于单选按钮的最佳方式是什么?

2 个答案:

答案 0 :(得分: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);
            }
        }
    });

答案 1 :(得分:0)

获取所选的单选按钮值

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" />
相关问题