gpt4 book ai didi

android - 使用按钮选择器禁用按钮

转载 作者:IT老高 更新时间:2023-10-28 23:29:59 25 4
gpt4 key购买 nike

我有一个按钮选择器,可以在按下按钮时更改按钮图像。我还设置了禁用按钮时的图像。我尝试以编程方式禁用该按钮,但未显示禁用的按钮图像。我的 button_selector 正确吗?

<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) 来禁用按钮

最佳答案

试试这个,我为你上传了一个示例项目以获得更多帮助,检查项目

选择器.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>

并在按钮中使用此选择器,如下所示

<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

关于android - 使用按钮选择器禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18515942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com