Android - Listen to a disabled button

前端 未结 8 2071
一整个雨季
一整个雨季 2021-01-17 08:56

How can I respond to an event based on clicking a disabled Button. I have a requirement that I have to present Dialog, when a disabled Button

相关标签:
8条回答
  • 2021-01-17 09:18

    I solved this issue by using a flag to keep my button's state.

    private boolean isMyButtonEnabled = false;
    
    public void onMyButtonClick(View v) {
       if(isMyButtonEnabled){
          ..
       }
    }
    
    0 讨论(0)
  • 2021-01-17 09:24

    You can for example use #setActivated() method instead. Disabling a view will ignore all events. https://developer.android.com/reference/android/view/View.html#setActivated(boolean). Then you can customize text and background styles with android:state_activate attribute if you need:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_activated="false"
          android:color="@color/a_color" />
        <item android:state_activated="true"
          android:color="@color/another_color" />
    </selector>
    
    0 讨论(0)
提交回复
热议问题