android RadioButton注意事項

用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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章