Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何处理在子视图中单击和在父视图组中触摸?_Android_Ontouchlistener_Viewgroup - Fatal编程技术网

Android 如何处理在子视图中单击和在父视图组中触摸?

Android 如何处理在子视图中单击和在父视图组中触摸?,android,ontouchlistener,viewgroup,Android,Ontouchlistener,Viewgroup,在我的布局中,我有这样一个结构: --RelativeLayout | --FrameLayout | --Button, EditText... 我想在RelativeLayout和FrameLayout中处理触摸事件,所以我在这两个视图组中设置了onTouchListener。但只有相对论中的接触被捕捉 为了解决这个问题,我编写了我自己的CustomRelativeLayout,并覆盖了onInterceptTouchEvent的,现在捕获了子视图组(FrameLa

在我的布局中,我有这样一个结构:

--RelativeLayout
  |
  --FrameLayout
    |
    --Button, EditText...
我想在RelativeLayout和FrameLayout中处理触摸事件,所以我在这两个视图组中设置了onTouchListener。但只有相对论中的接触被捕捉

为了解决这个问题,我编写了我自己的CustomRelativeLayout,并覆盖了onInterceptTouchEvent的
,现在捕获了子视图组(FrameLayout)中的单击,但是按钮和其他视图中的单击没有任何效果

在我自己的自定义布局中,我有:

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

您的自定义解决方案将从相对布局中的任何位置捕获触摸事件,因为覆盖的方法设置为always throw true

对于你的要求,我想最好使用 使用onClick方法,而不是使用onTouch

OnTouch方法在每个TouchEvent上调用不同的线程,我想这就是问题的原因


与其处理这些事件,不如尝试onClick方法。

您需要为每个子级重写
onInterceptTouchEvent()
,否则它将仍然是父级的
onTouchEvent


您需要返回false让子级处理它,否则您将返回给父级。

我可以用以下代码解决此问题:

步骤1:在onCreate()方法上方声明EditText

步骤2:在onResume()方法中,配置结束:

etMyEdit = (EditText) findViewById (R.id.editText);

etMyEdit.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_UP:
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
            }
            return false;
        }
    });

希望它能帮助别人

谢谢,但即使我在两个视图组中设置了onClick,也只会处理第一个视图组中的单击(RelativeLayout)。共享代码肯定会让事情变得更清楚。假设我只想覆盖触摸事件来处理一些子视图组,我可以在这个函数中做些什么来让它工作?我的意思是,对于一些孩子来说,它会像往常一样工作,而对于一些孩子来说,家长的观点将决定他们是否会得到触摸事件。
public EditText etMyEdit;
etMyEdit = (EditText) findViewById (R.id.editText);

etMyEdit.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_UP:
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
            }
            return false;
        }
    });