Android Layout make all children's not clickable

后端 未结 12 2157
一生所求
一生所求 2021-01-01 12:45

I am using Relative Layout and many buttons in it with TextViews etc.I want to make all of them not clickable unless some event happens.

12条回答
  •  Happy的楠姐
    2021-01-01 13:13

    A very simple and full-proof way to do it is to create a sub class and override onInterceptTouchEvent:

    public class MyRelativeLayout extends RelativeLayout {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            // true if you do not want the children to be clickable.
            return mShouldInterceptAllTouch;
        }
    }
    

    No need to call any of the children's methods.

    You can still call setOnClickListener on your myRelativeLayout object. Also, you can use the class in XMLs as if you were using a RelativeLayout

提交回复
热议问题