使用按钮选择器禁用按钮 [英] Disable button with button selector
问题描述
我有一个按钮选择器,可以在按下按钮时更改按钮图像。我还为禁用按钮设置了图像。我尝试以编程方式禁用按钮,但是未显示禁用的按钮图像。我的button_selector是否正确?
I have a button selector that changes the button image when it is pressed. I have also set an image for when the button is disabled. I try and disable the button programmatically but the disabled button image is not appearing. Is my button_selector correct?
<item android:drawable="@drawable/red_btn_bg_disabled" android:state_enabled="false"/> <!-- disabled -->
<item android:drawable="@drawable/red_btn_bg_pressed" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="@drawable/red_btn_bg_pressed" android:state_focused="true"/> <!-- focused -->
<item android:drawable="@drawable/red_btn_bg"/> <!-- default -->
我正在使用mButton.setEnabled (false)在我的代码中以禁用按钮
I am using mButton.setEnabled(false) in my code to disable the button
推荐答案
尝试这一操作,我为您上传了一个示例项目以获取更多帮助项目
try this one and i uploaded one sample project for you for more help check the project
selector.xml
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_disable" android:state_enabled="false"/>
<item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/btn_normal"/>
</selector>
并按如下所示在按钮中使用此选择器
and use this selector in button as following
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="30dp"
android:background="@drawable/selector"
android:enabled="false"
android:text="Disable Button" />
示例代码链接
https://www.dropbox.com/s/lydkog10rkujbsa/ButtonSelector.rar
这篇关于使用按钮选择器禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!