- 千万不要在代码控制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"/>
====================================
2. 好的,今天才发现,原来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);
但是以上这种方法会导致onCheckedChanged多次调用。
===============================
3. 为了防止onCheckedChanged被多次调用,最终使用终极简单粗暴的方法解决了。两个图片,表示选中与未选中状态,然后通过逻辑控制这两种状态显示与隐藏。
===============================
4. 换一种思路:可以在方法2中改变radiobutton的状态之前,尝试取消监听,等执行完setChecked方法,等radiobutton的状态和checked值同步之后,再添加监听,或许可行,同学们可以一试。
5. 换一种思路2:用ViewPagerIndicator代替。