Disable a whole activity from user action

后端 未结 7 650
再見小時候
再見小時候 2020-12-02 09:17

Is there a simple way to disable a user interacting with an activity. To be done when there is an action running (and a spinning progress bar in the title bar)

EDIT:

7条回答
  •  失恋的感觉
    2020-12-02 09:49

    If you need to disable event processing for a period of time (for instance, while you run an animation, show a waiting dialog), you can override the activity's dispatch functions.

    To disable touch/clicks on any buttons, add these members/functions to your activity:

    protected boolean enabled = true;
    
    public void enable(boolean b) {
        enabled = b;
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return enabled ? 
            super.dispatchTouchEvent(ev) : 
            true;        
    }
    

    Then just call enable(true/false) when you need to disable and enable the activity's normal event handling.

提交回复
热议问题