Android Layout make all children's not clickable

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

提交回复
热议问题