Change background for radio button programatically

孤街浪徒 提交于 2019-12-24 09:44:13

问题


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?


回答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:


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


来源:https://stackoverflow.com/questions/48155347/change-background-for-radio-button-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!