如何在父视图的触摸监听器中消耗子视图的触摸事件?

20
在我的应用程序中,我想在父视图的 onTouchListener 中获取所有子视图的触摸事件,但我无法实现这个目标。 示例: 在我的 Activity 视图中,我有一个帧布局,其中包含一些按钮和编辑文本。因此,每当我点击按钮或编辑文本时,我想在 帧布局的 onTouchListeners 中获取该触摸事件。
以下是我的代码... Activity:
private FrameLayout rootView;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        rootView = (FrameLayout) findViewById(R.id.rootview);
        rootView.setOnTouchListener(new OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                Log.e("Touch Event: ", " X: " + event.getX() + " Y: " + event.getY());
                return false;
            }
        });
         }

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:id="@+id/rootview">

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:layout_gravity="center" 
    android:padding="10dip">

     <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:padding="4dp"
                    android:text="Login"
                    android:textColor="@android:color/black"
                    android:textSize="16dp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Username"
                    android:textColor="@android:color/black"
                    android:textSize="14dp"
                    android:textStyle="bold" />

                <EditText
                    android:id="@+id/edttextusername"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:imeOptions="flagNoExtractUi"
                    android:singleLine="true" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Password"
                    android:textColor="@android:color/black"
                    android:textSize="14dp"
                    android:textStyle="bold" />

                <EditText
                    android:id="@+id/edttextpassword"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:imeOptions="flagNoExtractUi"
                    android:password="true" />

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:orientation="horizontal"
                    android:padding="10dp" >

                    <Button
                        android:id="@+id/btnlogin"
                        android:layout_width="0dip"
                        android:layout_height="40dip"
                        android:layout_marginRight="5dp"
                        android:layout_weight="1"
                        android:enabled="false"
                        android:text="Login"
                        android:padding="5dip"
                        android:textColor="@android:color/black"
                        android:textStyle="bold" 
                        />

                    <Button
                        android:id="@+id/btncall"
                        android:layout_width="0dip"
                        android:layout_height="40dip"
                        android:layout_marginLeft="5dp"
                        android:layout_weight="1"
                        android:padding="5dip"
                        android:text="Cancel"
                        android:textColor="@android:color/black"
                        android:textStyle="bold" />
                </LinearLayout>


</LinearLayout>

    </FrameLayout> 

问题:每当我触摸按钮或编辑文本时,我无法在我的帧布局的onTouchListener中获取触摸坐标(它没有被调用)。

注意:我不想为所有子视图添加单独的onTouchListener。

提前致谢。

4个回答

24

通过在布局中覆盖dispatchTouchEvent方法即可实现该功能。

public class MyFrameLayout extends FrameLayout {
    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        // do what you need to with the event, and then...
        return super.dispatchTouchEvent(e);
    }
}

然后将该布局替换为通常的FrameLayout:

<com.example.android.MyFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:id="@+id/rootview">
  ...

如果需要防止子View接收事件,您也可以完全跳过super调用,但我认为这很少见。如果您需要重新创建一些默认功能而非全部,则需要重写大量代码:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/view/ViewGroup.java#ViewGroup.dispatchTouchEvent%28android.view.MotionEvent%29


3

有两种解决方法:

  1. 使用ViewGroup上的onInterceptTouchEvent,如此处所示。

  2. 避免布局处理触摸事件,并使一个视图成为布局的子视图,覆盖其整个大小以处理触摸事件。

    这不像扩展类那样高效,但更容易且不需要创建新文件/类。

    例如:

    只需将触摸事件添加到该视图中,它就会处理它们而不是其他任何视图。


3
我曾经尝试过类似的设计(在根FrameLayout上只有一个onTouch监听器),并且它起作用了。只要onTouch返回true而不是false,我就能获得所有的点。否则,我只能得到第一个点。由于我期望相反的行为,所以我无法找出这个“返回”问题的原因。
然而,根据我的经验,如果您设置任何子视图可点击,则其父级的onTouch将不起作用。如果您有其他解决方案并分享给我,那将是非常好的。

-3
可以通过使用“onInterceptTouchEvent”来实现。请尝试一下,可能会起作用。
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
     onTouchEvent(ev);
     return  false;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接