如何在父视图的触摸侦听器中使用子视图的触摸事件? [英] How to consume child view's touch event in parent view's touchlistener?

查看:29
本文介绍了如何在父视图的触摸侦听器中使用子视图的触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想获取父视图 onTouchListener 中所有子视图的触摸事件,但我无法获取.

In my application I want to get the touch event of all child view's in my parent view's onTouchListener but I could not get this.

示例:

在我的活动视图中,我有一个框架布局,其中包含一些按钮和编辑文本.因此,每当我触摸按钮或编辑文本时,我都希望在 framlayout's onTouchListeners 中获得此触摸事件.

In my activity's view I have one frame layout which has some buttons and edittexts in it. So whenever I touch buttons or edit text I want to get this touch event in framlayout's onTouchListeners.

这是我的代码..

活动:

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

ma​​in.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(未调用)中获得触摸协调.

Problem: So whenver I touch the buttons or edittext I can't get touch co-ordition in my frame layout's onTouchListener (It's not called).

注意:我不想为所有 childView 添加单独的 onTouchListener.

Note: I don't want to add separate onTouchListener for all childViews.

提前致谢.

推荐答案

您可以通过覆盖 dispatchTouchEvent 在布局中.

You could accomplish that by overriding dispatchTouchEvent in the layout.

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:

Then use that layout in place of the usual 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">
  ...

如果您需要阻止子视图接收事件,您也可以完全跳过超级调用,但我认为这种情况很少见.如果您需要重新创建一些(但不是全部)默认功能,则有很多需要重写:

You could also skip the super call entirely if you need to prevent child views from receiving the event, but I think this would be rare. If you need to recreate some, but not all, of the default functionality, there is a lot to rewrite:

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

这篇关于如何在父视图的触摸侦听器中使用子视图的触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆