Disable the touch events for all the views

前端 未结 13 1379
暗喜
暗喜 2020-11-28 06:53

What\'s the best way to disable the touch events for all the views?

相关标签:
13条回答
  • 2020-11-28 06:54

    What about covering a transparent view over all of your views and capturing all touch event?

    0 讨论(0)
  • 2020-11-28 06:56

    Override the dispatchTouchEvent method of the activity and like this:

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){      
      return true;//consume
    }
    

    If you return true all touch events are disabled.

    Return false to let them work normally

    0 讨论(0)
  • 2020-11-28 06:57

    Per your comment:

    i just want to be able to disable the views of the current activity at some point

    you seem to want to disable all touch for the current activity regardless of the view touched.

    Returning true from an override of Activity.dispatchTouchEvent(MotionEvent) at the appropriate times will consume the touch and effectively accomplish this. This method is the very first in a chain of touch method calls.

    0 讨论(0)
  • 2020-11-28 06:58

    One more easier way could be disabling it through layout (i.e. .xml) file: Just add

     android:shouldDisableView="True"
    

    for the view you want disable touch events.

    0 讨论(0)
  • 2020-11-28 06:59
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                         WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    
    0 讨论(0)
  • 2020-11-28 06:59

    The easiest way to do this is

    private fun enableDisableActivty(isEnable : Boolean){
                if(!isEnable) window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
                else window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
        } 
    
    0 讨论(0)
提交回复
热议问题