Disable button with button selector

前端 未结 2 1627
难免孤独
难免孤独 2021-02-12 20:35

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 programmaticall

相关标签:
2条回答
  • 2021-02-12 20:47

    Try this.

    <item android:drawable="@drawable/red_btn_bg_pressed" android:state_pressed="true"/> <!-- pressed -->
    
    <item android:drawable="@drawable/red_btn_bg_disabled" android:state_enabled="false"/> <!-- disabled -->
    
    <item android:drawable="@drawable/red_btn_bg"/> <!-- default -->
    
    0 讨论(0)
  • 2021-02-12 20:56

    try this one and i uploaded one sample project for you for more help check the project

    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" />
    

    sample code link https://www.dropbox.com/s/lydkog10rkujbsa/ButtonSelector.rar

    0 讨论(0)
提交回复
热议问题