首页 > 学院 > 开发设计 > 正文

RadioButton调用setChecked()不起作用

2019-11-07 23:40:29
字体:
来源:转载
供稿:网友

千万不要在代码控制RadioButton的状态,如果在代码控制,虽然实际状态变化了,但是UI表现上却没有变化。使用RadioButton你只需监听它的状态变化然后作出相应的操作,如果你想要你的RadioButton拥有CheckBox的特性,请毫不犹豫选择CheckBox,并且为CheckBox设置主题为RadioButton的主题即可。 然后你就可以在代码调用setChecked方法改变状态了(UI和状态同步)。

<CheckBox android:id="@+id/cb_others" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:drawablePadding="@dimen/margin_small" android:text="代提" android:textColor="@color/black_text" style="@android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton"/>

==================================== 好的,今天才发现,原来drawablestate和check不同步是可以解决的,只是在UI上闪一下,比如:你发现rb_self的drawablestate和check不同步,那么调用refreshdrawablestate()方法是不起作用的,你需要设置一下其他的RadioButton的状态,然后才能正确设置目标RadioButton的状态。设置rb_others.setChecked(true);的方法如下:

rb_self.setChecked(false);rb_self.setChecked(true);rb_self.setChecked(false);rb_others.setChecked(true);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表