小编Soc*_*ock的帖子

如何在父视图的touchlistener中使用子视图的触摸事件?

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

例:

在我的活动视图中,我有一个框架布局,其中包含一些按钮和edittexts.因此,每当我触摸按钮或编辑文本时,我都希望在framlayout中 获得此触摸事件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;
            }
        });
         }
Run Code Online (Sandbox Code Playgroud)

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" …
Run Code Online (Sandbox Code Playgroud)

android view touch

19
推荐指数
2
解决办法
3万
查看次数

标签 统计

android ×1

touch ×1

view ×1