android radiobutton切换,radiobutton 点击无法切换

就像平时写的代码一样radiogroup 里面放两个radiobutton,代码如下

android:id="@+id/rb_over_rest"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:padding="@dimen/dp_4"

app:layout_constraintTop_toBottomOf="@+id/line1">

style="@style/MyRadioButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="true"

android:tag="2"

android:text="加班(+)"

android:textColor="@drawable/select_check_yellow" />

style="@style/MyRadioButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:tag="3"

android:text="请假(—)"

android:textColor="@drawable/select_check_yellow" />

颜色

但是实际ui点击却只生效一次,再次点击却不生效,最后和以前代码一点点比对,一点点测试才发现是因为RadioButton没有定义id,加上id 就好了。

翻开RadioGroup 源码 查看check方法

还有这行代码

int value = attributes.getResourceId(R.styleable.RadioGroup_checkedButton, View.NO_ID);

if (value != View.NO_ID) {

mCheckedId = value;

mInitialCheckedId = value;

}

public void check(@IdRes int id) {

// don't even bother

if (id != -1 && (id == mCheckedId)) {

return;

}

if (mCheckedId != -1) {

setCheckedStateForView(mCheckedId, false);

}

if (id != -1) {

setCheckedStateForView(id, true);

}

setCheckedId(id);

}

可以知道底层RadioGroup 是获取了点击的radiobutton 的id的,所以才会出现我这样的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值