Android Layout make all children's not clickable

后端 未结 12 2207
一生所求
一生所求 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条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 13:25

    If you want the children of a ViewGroup to be unresponsive to touch events, but you want the ViewGroup itself to respond to clicks, for example, you can create your own ViewGroup subclass, override onInterceptTouchEvent(), and always return true. This will intercept all touch events before children see them, while allowing your custom ViewGroup to remain responsive to touch events.

    So, instead of RelativeLayout, you could use your own subclass:

    public class ControlFreakRelativeLayout extends RelativeLayout {
        private boolean mWithholdTouchEventsFromChildren;
    
        // Constructors omitted for sake of brevity
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            return mWithholdTouchEventsFromChildren || super.onInterceptTouchEvent(ev);
        }
    
        public void setWithholdTouchEventsFromChildren(boolean withholdTouchEventsFromChildren) {
            mWithholdTouchEventsFromChildren = withholdTouchEventsFromChildren;
        }
    }
    

提交回复
热议问题