android应用中的自定义单选按钮不会在禁用该按钮时变灰 [英] Custom Radio button in android app doesn't get grayed out on disabling that

查看:98
本文介绍了android应用中的自定义单选按钮不会在禁用该按钮时变灰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android中有一个自定义单选按钮.它的作品很好.我的问题是,当我禁用这些按钮时,它不会显示为灰色.我该怎么办?这是我的自定义单选按钮的示例代码.

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.

推荐答案

请注意选择器项中的属性,因为它是逻辑AND.

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

表示是否在项目中放入了 android:state_checked ="false" android:state_enabled ="true" .只有当两者都为真时,它才起作用.下面的代码在选中未选中已禁用状态下正常工作.

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>

这篇关于android应用中的自定义单选按钮不会在禁用该按钮时变灰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆