Change background for radio button programatically

2019-08-02 11:54发布

I have a RadioGroup with two RadioButtons. I want to change the color of them programmatically when they are disabled.

   <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>

I have created new XML selectors with gray colors but when I set the background it does not work.

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>

I tried

national_id_docs.setBackground(R.drawable.radio_btn_selector_left_disabled);

could you suggest what is the best way to use the radio_btn_selector_left_disabled for the radio button?

2条回答
看我几分像从前
2楼-- · 2019-08-02 11:56
    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);
            }
        }
    });
查看更多
我命由我不由天
3楼-- · 2019-08-02 12:21

To get your selected radio button value

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

Solution 1: Change colorAccent color

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

Solution 2: Create your style in your style.xml and extend it to your radio button.

<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"/>

Solution 3: Use 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" />
查看更多
登录 后发表回答