How to disable all click events of a layout?

后端 未结 7 1751
不知归路
不知归路 2020-12-16 10:51

I have a layout that contains many views. Is there an easy way to disable all its views click events?

相关标签:
7条回答
  • 2020-12-16 11:09

    You need to call setEnabled(boolean value) method on the view.

    view.setClickable(false);
    view.setEnabled(false);
    
    0 讨论(0)
  • 2020-12-16 11:18

    I would create a ViewGroup with all the views that you want to enable/disable at the same time and call setClickable(true/false) to enable/disable clicking.

    0 讨论(0)
  • 2020-12-16 11:21

    I would implement onClickListener interface in your activity class and return false in onClick method. I feel it's the easiest way to solve your problem.

    0 讨论(0)
  • 2020-12-16 11:23

    Here is a Kotlin extension function implementation of Parag Chauhan's answer

    fun View.setAllEnabled(enabled: Boolean) {
        isEnabled = enabled
        if (this is ViewGroup) children.forEach { child -> child.setAllEnabled(enabled) }
    }
    
    0 讨论(0)
  • 2020-12-16 11:30

    Rather than iterating through all the children view, you can add this function to the parent Layout view

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
    

    This will be called before the onTouchEvent for any child views, and if it returns true, the onTouchEvent for child views wont be called at all. You can create a boolean field member to toggle this state on and off if you want.

    0 讨论(0)
  • 2020-12-16 11:31

    You can pass View for disable all child click event.

    public static void enableDisableView(View view, boolean enabled) {
            view.setEnabled(enabled);
            if ( view instanceof ViewGroup ) {
                ViewGroup group = (ViewGroup)view;
    
                for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
                    enableDisableView(group.getChildAt(idx), enabled);
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题