Android Layout make all children's not clickable

后端 未结 12 2067
一生所求
一生所求 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:20

    loop on your buttons and use setClickable function and pass false value for it. then when your even happen loop again on ot and setClickable to true

    0 讨论(0)
  • 2021-01-01 13:21

    I found an alternative way to achieve this. You may create a blocking LinearLayout on top all its sibling views in the RelativeLayout like below:

    <RelativeLayout
        android:id="@+id/rl_parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- the children views begins -->
        ...
        <!-- the children views ends -->
    
        <LinearLayout
            android:id="@+id/ll_mask"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:clickable="true"
            android:orientation="horizontal"
            android:visibility="visible"/>
    
    </RelativeLayout>
    

    Later you may toggle the LinearLayout's visibility to GONE to allow the children views to be clickable again:

    mMask.setVisibility(View.VISIBLE); // blocks children
    mMask.setVisibility(View.GONE); // children clickable
    
    0 讨论(0)
  • 2021-01-01 13:22

    An easy Kotlin extension solution to disable/enable a view and all of it's children:

    fun View.isUserInteractionEnabled(enabled: Boolean) {
        isEnabled = enabled
        if (this is ViewGroup && this.childCount > 0) {
            this.children.forEach {
                it.isUserInteractionEnabled(enabled)
            }
        }
    }
    

    and call it with:

    view.isUserInteractionEnabled(false)
    
    0 讨论(0)
  • 2021-01-01 13:25

    Since you are going to perform the click for some items in the layout when that particular function is executed,

    • You can keep a static flag like a boolean
    • Create a static boolean globally and declare it as false, once the particular function is performed change it to true.
    • so in all the onclick functions that u r performing check this flag if it is true perform the necessory function.
    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2021-01-01 13:25

    You can make a common method in which you can write the code for disabling all your views inside your relative layout and call this method in onCreate() and then you can enable your particular view inside the the layout on your event.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题