2013-02-26 87 views
0

我有这个选择对我的按钮的背景选择禁用钮

<?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。

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

+0

一个问题在这里:'安卓:state_pressed'的第一个项目 - 是不是强制性的?你有另外一个名为“bg_botton_disable”的布局吗? – g00dy 2013-02-26 09:56:14

回答

0

重新排序您选择的项目

<?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>