onTouchEvent() will not be triggered if setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) is invoked

后端 未结 8 681
有刺的猬
有刺的猬 2021-02-01 16:11

I call

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) 

when my app starts to make my app able to displa

8条回答
  •  花落未央
    2021-02-01 16:24

    The method Activity.onTouchEvent() gets called at the end of the responder chain (meaning after all other views have had a chance to consume the event). If you tap on a view that is interested in touch (i.e. a Button or EditText) there's a good chance your Activity will never see that event.

    If you want to have access to touches before they every get dispatched to your view(s), override Activity.dispatchTouchEvent() instead, which is the method called at the beginning of the event chain:

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //Check the event and do magic here, such as...
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
    
        }
    
        //Be careful not to override the return unless necessary
        return super.dispatchTouchEvent(event);
    }
    

    Beware not to override the return value of this method unless you purposefully want to steal touches from the rest of the views, an unnecessary return true; in this spot will break other touch handling.

提交回复
热议问题