android RadioButton注意事项

来源:互联网 发布:阿里云备案幕布多少钱 编辑:程序博客网 时间:2024/05/22 04:56

用RadioButton做个小控件,用selector作了个背景,selector里item的声明竟然要有顺序,好恶心。所以以后遇到定义的selector不管用 得注意下

<?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/btn_left_selected"/><item android:state_pressed="true" android:drawable="@drawable/btn_left_selected"/><item android:drawable="@drawable/btn_left"/></selector>

如果第三个item你放到第一个位置的话 你选择某个RadioButton,他的背景色不会变,我这里没有一一去试,遇到情况再说


还有如果不要默认的小圆圈背景,在RadioButton控件里加个属性 android:button="@null",使其背景属性为透明


此外,RadioButton的checked属性设置为true,代码里调用RadioButton的check(id)方法,不会触发onCheckedChanged事件


至于为什么 我在api文档里找到解释 

the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.


http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList


Note: Remember that Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your default value should always be last (as demonstrated in the following example).


博客已搬到 http://marspring.mobi/