Custom Radio button in android app doesn't get grayed out on disabling that

泄露秘密 提交于 2019-12-11 05:37:53

问题


I have a custom radio button in android. Its works fine. My problem is that When I make those button disable it will not grayed out. How can I do so? Here is my sample code for custom radio button.

<?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/radio_select" />
 <item android:state_checked="false" android:drawable="@drawable/radio_holo" />
</selector>

Here is code which I use in my layout

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/custom_radio_button"
    android:onClick="OnClick"
    android:text="button 1"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/black" />

One more thing that I required text appearance small and normal text color black.


回答1:


Try adding this to your custom_radio_button.xml:

<item android:state_enabled="false" android:drawable="@drawable/your_grayed_out_btn" />



回答2:


If you need to change text color depending on the state of your RadioButton, try writing a selector as shown in this answer.




回答3:


Be careful with attributes in the selector item, as it is logical AND.

Means if you put android:state_checked="false" and android:state_enabled="true" in an item. It will only work when both are true. Below code is working fine for selected, unselected and disabled states.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/ic_radio_btn_inactive" android:state_checked="false" android:state_enabled="true"/>
   <item android:drawable="@drawable/ic_radio_btn_active" android:state_checked="true" android:state_enabled="true"/>
   <item android:drawable="@drawable/ic_radio_btn_diabled"    android:state_enabled="false"/>
</selector>


来源:https://stackoverflow.com/questions/15698586/custom-radio-button-in-android-app-doesnt-get-grayed-out-on-disabling-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!