如何设置RadioButton的背景颜色



您能告诉我如何设置RadioButton的背景颜色吗?
如果用户选择,则背景颜色应更改为绿色。
当用户选择时,背景颜色应更改为红色。

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <include layout="@layout/header" />
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="20dp"
        android:stretchColumns="2" >
        <TableRow android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFF00" >
            <TextView
                android:layout_height="wrap_content"
            android:layout_width="0dp"
                android:padding="5dp"
                android:text="Eligibility confirmed:" 
                android:layout_weight=".5"/>
            <RadioGroup
                android:id="@+id/eligibility"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:orientation="horizontal"
                android:layout_weight=".5" >
                <RadioButton
                    android:id="@+id/eligibilityYes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="yes" />
                <RadioButton
                    android:id="@+id/eligibilityNo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="No" />
            </RadioGroup>
        </TableRow>
        <TableRow
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="#FFFF00" >
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="Informed consent given and explained:"
              android:layout_weight=".5"
               />
            <RadioGroup
                android:id="@+id/explained"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal" 
                android:layout_weight=".5">
                <RadioButton
                    android:id="@+id/explainedYes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="yes" />
                <RadioButton
                    android:id="@+id/explainedNo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="No" />
            </RadioGroup>
        </TableRow>
    </TableLayout>
    <!--
         <Button 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="ClickMe"/>
    -->
</LinearLayout>

您应该尝试这种方式。希望这对您有帮助....

<RadioGroup
            android:id="@+id/eligibility"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:orientation="horizontal"
            android:layout_weight=".5" >
            <RadioButton
                android:id="@+id/eligibilityYes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:background="@drawable/checkbox_background"
                android:text="yes" />
            <RadioButton
                android:id="@+id/eligibilityNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_background"
                android:text="No" />
        </RadioGroup>

您的checkbox_background.xml文件将图像放入Res/drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/radio_rad" />
    <item android:state_checked="true" android:drawable="@drawable/radio_green" />
</selector>

android:radiobutton的按钮参数。示例 -

 <RadioButton
                android:id="@+id/rb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/radio_button_selector"
                android:textColor="@color/white" />

radio_button_selector.xml-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/radio_button" android:state_checked="false"/>
    <item android:drawable="@drawable/radio_button_dark" android:state_checked="true" />
</selector>

在RadioButton上检查了侦听器,如果已选中广播按钮,则可以设置单位butial.setbackground(..)

尝试这个...

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
            if(radioButton.getText().equals("yes")) {
                radioButton.setBackgroundColor(Color.GREEN);
            } else {
                radioButton.setBackgroundColor(Color.RED);
            }
        }
    });

最新更新