我有这个RadioGroup:
<RadioGroup
android:id="@+id/radioGroupStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/disclosure_button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
<RadioButton
android:id="@+id/radio_de_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/button_sel"
android:button="@null"
android:drawableRight="@drawable/german_flag"
android:state_checked="@color/blue"
/>
<RadioButton
android:id="@+id/radio_en_start"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:button="@null"
android:checked="true"
android:background="@drawable/button_sel"
android:layout_height="wrap_content"
android:drawableRight="@drawable/english_flag"
android:state_checked="@color/blue"/>
<RadioButton
android:id="@+id/radio_tr_start"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:background="@drawable/button_sel"
android:layout_height="wrap_content"
android:button="@null"
android:drawableRight="@drawable/turkish_flag"
android:state_checked="@color/blue"/>
</RadioGroup>
我希望在点击一个时突出显示它。最初的RadioButtons消失了。我已经尝试过这个选择器,但它不起作用......
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/blue"
android:state_focused="true"
android:state_pressed="true"/>
<item
android:drawable="@color/blue"
android:state_focused="false"
android:state_pressed="true"/>
<item
android:drawable="@android:color/transparent"
android:state_focused="true"
android:state_pressed="false"/>
<item
android:drawable="@android:color/transparent"
android:state_focused="false"
android:state_pressed="false"/>
</selector>
如果在XML中单击按钮而不是代码后面的每个按钮,我该怎么做?
答案 0 :(得分:0)
您似乎已将选择器属性android:state_checked="true"
放置在布局中而不是drawable中。您应该从布局中删除该属性:
<RadioButton [...]
的 android:state_checked="@color/blue"
/>
并且选择器状态指向您的可绘制资源(此处它将显示红色背景下的已检查项目):
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/red"
android:state_checked="true"/>
<item
android:drawable="@color/blue"
android:state_focused="true"
android:state_pressed="true"/>
<item
android:drawable="@color/blue"
android:state_focused="false"
android:state_pressed="true"/>
<item
android:drawable="@android:color/transparent"/>
</selector>
请注意,即使按下第一个选择器项目优先,已检查项目的背景也将保持红色。您可能需要考虑为已检查和关注/按下的项目添加其他规则。