【问题标题】:Android - RadioButton isn't uncheck with initial checked stateAndroid - RadioButton 未取消选中初始选中状态
【发布时间】:2012-07-15 08:34:04
【问题描述】:

我在 RadioGroup 中有一个 RadioButton,

当我设置按钮的初始状态时出现问题

android:checked = "true"

因为如果我按下单选按钮“F”,单选按钮“M”不会取消选中...

我该怎么办?怎么了?

代码如下:

<RadioGroup
   android:id="@+id/registrazione_utente_sesso"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >

   <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="true"
      android:text="M"
      android:textColor="#ff7415"
      android:textSize="18sp" />

   <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="13.33dp"
      android:text="F"
      android:textColor="#ff7415"
      android:textSize="18sp" />
</RadioGroup>

屏幕:

初始状态(正确):

http://i.imgur.com/YsUIg.png

当我按下 RadioButton "F" 时的最终状态(错误):

http://i.imgur.com/YJms9.png

谢谢

【问题讨论】:

  • 要设置按钮的初始状态需要手动管理
  • 你必须设置如果 F 选中而不是 M 未选中

标签: android radio-button radio-group


【解决方案1】:

使用 android:id 为单选按钮分配一个唯一的 id,然后设置 RadioGroup 的 android:checkedButton 属性,如下所示:

<RadioGroup
   android:id="@+id/registrazione_utente_sesso"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:checkedButton="@+id/radiobutton_m" >

   <RadioButton
      android:id="@+id/radiobutton_m"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="M"
      android:textColor="#ff7415"
      android:textSize="18sp" />

   <RadioButton
      android:id="@+id/radiobutton_f"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="13.33dp"
      android:text="F"
      android:textColor="#ff7415"
      android:textSize="18sp" />
</RadioGroup>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2019-05-08
    • 1970-01-01
    相关资源
    最近更新 更多