[翻译]  Disable the touch events for all the views

[CHINESE]  禁用所有视图的触摸事件


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

禁用所有视图的触摸事件的最佳方法是什么?

Thanks a lot,

非常感谢,

Gratzi

的Gratzi

11 个解决方案

#1


52  

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);
      }
    }
  }

#2


15  

Override the dispatchTouchEvent method of the activity and like this:

覆盖活动的dispatchTouchEvent方法,如下所示:

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

If you return true all touch events are disabled.

如果返回true,则禁用所有触摸事件。

Return false to let them work normally

返回false以让它们正常工作

#3


13  

You could try:

你可以尝试:

your_view.setEnabled(false);

Which should disable the touch events.

哪个应该禁用触摸事件。

alternatively you can try (thanks to Ercan):

或者你可以试试(感谢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);
}

#4


8  

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.

当且仅当inProgress变量设置为false时,这段代码才会将此事件传播到父视图,从而允许触摸事件。

private boolean inProgress = false;

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (!inProgress)
        return super.dispatchTouchEvent(ev);
    return true;
 }

#5


3  

What about covering a transparent view over all of your views and capturing all touch event?

如何覆盖所有视图的透明视图并捕获所有触摸事件?

#6


3  

Use this. returning true will indicate that the listener has consumed the event and android doesn't need to do anything.

用这个。返回true表示监听器已经使用了该事件,并且android不需要做任何事情。

view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

#7


2  

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

整个申请可能无法实现。您必须为每个视图覆盖onTouchEvent()并忽略用户输入。

#8


1  

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);
        }
    }
}

#9


1  

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                     WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

#10


0  

Per your comment:

根据你的评论:

i just want to be able to disable the views of the current activity at some point

我只是希望能够在某些时候禁用当前活动的视图

you seem to want to disable all touch for the current activity regardless of the view touched.

您似乎想要禁用当前活动的所有触摸,无论触摸的视图如何。

Returning true from an override of Activity.dispatchTouchEvent(MotionEvent) at the appropriate times will consume the touch and effectively accomplish this. This method is the very first in a chain of touch method calls.

在适当的时间从Activity.dispatchTouchEvent(MotionEvent)的重写返回true将消耗触摸并有效地完成此操作。这种方法是触摸方法调用链中的第一个。

#11


0  

One more easier way could be disabling it through layout (i.e. .xml) file: Just add

另一种更简单的方法是通过布局(即.xml)文件禁用它:只需添加即可

 android:shouldDisableView="True"

for the view you want disable touch events.

对于您想要禁用触摸事件的视图。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号