选择器禁用botton(selector disable botton)

编程入门 行业动态 更新时间:2024-10-23 15:19:25
选择器禁用botton(selector disable botton)

我有一个选择器用于我的按钮背景之一

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
          android:drawable="@drawable/bg_botton_disable"/>
    <item android:state_pressed="true"
          android:state_enabled="true"  
          android:drawable="@drawable/bg_botton_pressed"/> <!-- pressed -->
    <item android:state_enabled="true" 
          android:drawable="@drawable/bg_botton" /> <!-- default -->
</selector>
 

(Button) bt = (Button) findviewById(R.id.MyBotton);
bt.setEnabled(false);
 

代码禁用了我的按钮的功能。 我的意思是当我按下按钮时它不会启动它自己的onClick()方法。 这很好,但问题是它不会将按钮的背景更改为bg_botton_disable drawable。

有没有人有任何建议如何解决它?

I have this selector for one of my button's background

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
          android:drawable="@drawable/bg_botton_disable"/>
    <item android:state_pressed="true"
          android:state_enabled="true"  
          android:drawable="@drawable/bg_botton_pressed"/> <!-- pressed -->
    <item android:state_enabled="true" 
          android:drawable="@drawable/bg_botton" /> <!-- default -->
</selector>
 

(Button) bt = (Button) findviewById(R.id.MyBotton);
bt.setEnabled(false);
 

The code disables the functionality of my button. I mean when I press the button it doesn't launch its own onClick() method. That's fine, but the problem is that it doesn't change the background of the button to bg_botton_disable drawable.

Does anyone has any suggestion how to fix it?

最满意答案

重新排序选择器项目

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:drawable="@drawable/bg_botton" /> <!-- default --> <item android:state_pressed="true" android:drawable="@drawable/bg_botton_pressed"/> <!-- pressed --> <item android:state_enabled="false" android:drawable="@drawable/bg_botton_disable"/> </selector>

Reorder your selector items

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:drawable="@drawable/bg_botton" /> <!-- default --> <item android:state_pressed="true" android:drawable="@drawable/bg_botton_pressed"/> <!-- pressed --> <item android:state_enabled="false" android:drawable="@drawable/bg_botton_disable"/> </selector>

更多推荐

drawable,按钮,android,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-07-04 17:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选择器   botton   disable   selector

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!