简体   繁体   English

单击时更改RadioButton的颜色

[英]Change the Color of RadioButton When Clicked

I have a RadioGroup having 3 Radio Buttons . 我有一个RadioGroup有3个Radio Buttons By default , when I click the radio button, it turns Blue . 默认情况下,当我单击单选按钮时,它会变为蓝色 I need to change the color, say , when I click the first one, it should turn to Yellow , the 2nd one Blue and the 3rd one to Green . 我需要更改颜色,比如,当我点击第一个时,它应该变为黄色 ,第二个变为蓝色 ,第三个变为绿色

I have seen some tutorials to customize by styling the radio buttons, like Is it possible to change the radio button icon in an android radio button group but it hides all the buttons. 我看过一些教程,通过设置单选按钮的样式来自定义,比如可以更改android单选按钮组中的单选按钮图标,但它隐藏了所有按钮。

在此输入图像描述

Here is my XML . 这是我的XML。

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/studentImage"
    android:layout_alignParentRight="true"
    android:layout_alignRight="@+id/studentImage"
    android:layout_alignTop="@+id/studentImage"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible" />

    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible" />

    <RadioButton
        android:id="@+id/rb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible" />
</RadioGroup>

You can use a statelist drawable like I wrote below: 你可以使用像我在下面写的那样绘制一个statelist:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/color_pressed" /> <!-- pressed -->
<item android:drawable="@drawable/default_button" /> <!-- default -->
</selector> 

color_pressed and deafult_button would be your drawables for each button's state, you could have multiple selectors for each radio button color_pressed和deafult_button将成为每个按钮状态的drawable,你可以为每个单选按钮设置多个选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM