0

我想从我的整个屏幕上收集所有可能的触摸。我实施了一个

@Override
   public boolean onTouchEvent(MotionEvent e)
    {
       double x = e.getX();
       double y = e.getY();
    }

但是现在我在“场景”中添加了一个 TableView,当我点击它时,上面的方法没有被调用,因为 TableView 正在吞噬 Touches。 http://i.gyazo.com/d671768c45a0d9576d6ba3b822dfa85d.png

黄色区域周围的所有内容都是可点击的,但黄色区域本身不可点击。我也希望它是可点击的,基本上我希望整个屏幕区域一直都是可点击的,这样我就可以存储触摸位置。怎么做?

编辑: onTouchEvent 方法在 MainActivity 类中。这是activity_main.xml

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:shrinkColumns="*"
            android:stretchColumns="*" >

            <TableRow android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/column1"
                    android:text="tapcount"
                    android:layout_height="wrap_content"
                    />
            </TableRow>

        </TableLayout>
    </ScrollView>
    </LinearLayout>
4

2 回答 2

0

用你自己的透明覆盖整个视图,如下所示:

<RelativeLayout>
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
            <!--your content inside-->
    </LinearLayout>
    <View
        android:id="@+id/cover"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>
</RelativeLayout>

然后将您的代码用于Viewid=cover 并始终false在您onTouchEvent的文档中返回:

如果事件已被处理,则返回 True,否则返回 false。

false硬编码MotionEvent将被传送到下面/下的视图

于 2015-06-10T13:58:13.983 回答
0

在你的onCreate()方法中尝试实现这样的东西

View contentView = (View) findViewById(R.id.linearLayout1); //Your layout
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){

                    Toast.makeText(getApplicationContext(), event.getX() + " " + event.getY(), Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });
于 2015-06-10T13:58:31.627 回答