Disable the touch events for all the views

前端 未结 13 1381
暗喜
暗喜 2020-11-28 06:53

What\'s the best way to disable the touch events for all the views?

相关标签:
13条回答
  • 2020-11-28 07:01

    It may not be possible for the whole application. You will have to override onTouchEvent() for each view and ignore the user inputs.

    0 讨论(0)
  • 2020-11-28 07:02

    This piece of code will basically propagate this event to the parent view, allowing the touch event, if and only if the inProgress variable is set to false.

    private boolean inProgress = false;
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!inProgress)
            return super.dispatchTouchEvent(ev);
        return true;
     }
    
    0 讨论(0)
  • 2020-11-28 07:09

    In Kotlin:

    fun View.setEnabledRecursively(enabled: Boolean) {
        isEnabled = enabled
        if (this is ViewGroup)
            (0 until childCount).map(::getChildAt).forEach { it.setEnabledRecursively(enabled) }
    }
    
    // usage
    import setEnabledRecursively
    
    myView.setEnabledRecursively(false)
    
    0 讨论(0)
  • 2020-11-28 07:14

    You could try:

    your_view.setEnabled(false);
    

    Which should disable the touch events.

    alternatively you can try (thanks to Ercan):

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){      
      return true;//consume
    }
    

    or

    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(!onInterceptTouchEvent()){
            for(View child : children){
                if(child.dispatchTouchEvent(ev))
                    return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    
    0 讨论(0)
  • 2020-11-28 07:14

    I made this method, which works perfect for me. It disables all touch events for selected view.

    public static void disableView(View v) {
    
        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                disableView(child);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 07:15

    Here is a function for disabling all child views of some view group:

     /**
       * Enables/Disables all child views in a view group.
       * 
       * @param viewGroup the view group
       * @param enabled <code>true</code> to enable, <code>false</code> to disable
       * the views.
       */
      public static void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
        int childCount = viewGroup.getChildCount();
        for (int i = 0; i < childCount; i++) {
          View view = viewGroup.getChildAt(i);
          view.setEnabled(enabled);
          if (view instanceof ViewGroup) {
            enableDisableViewGroup((ViewGroup) view, enabled);
          }
        }
      }
    
    0 讨论(0)
提交回复
热议问题