【问题标题】:Change the Color of RadioButton When Clicked单击时更改 RadioButton 的颜色
【发布时间】:2014-09-30 09:12:32
【问题描述】:

我有一个 RadioGroup 有 3 个 Radio Buttons。默认情况下,当我单击单选按钮时,它会变为蓝色。我需要改变颜色,比如说,当我点击第一个时,它应该变成Yellow,第二个Blue,第三个变成Green。

我看过一些通过设置单选按钮样式来自定义的教程,例如Is it possible to change the radio button icon in an android radio button group,但它隐藏了所有按钮。

这是我的 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>

【问题讨论】:

  • 您可以尝试 this 为所有单选按钮使用不同的选择器。
  • @PerlDeveloper,感谢链接,它对我有用。
  • @Aleena 我也在寻找同样的信息。您能否就如何实现这一目标提出建议? PearlDeveloper 似乎已删除您认为有用的评论...

标签: android android-radiogroup android-radiobutton


【解决方案1】:

你可以像我在下面写的那样使用 statelist drawable:

<?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 和 deafult_button 将是每个按钮状态的可绘制对象,每个单选按钮可以有多个选择器

【讨论】:

  • 当我将此样式应用到我的单选按钮(如 android:button="@drawable/radio" )时,它会隐藏按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 2021-07-07
  • 1970-01-01
相关资源
最近更新 更多