【问题标题】:How to handle multitouch events in Android with multiple nested layouts?如何在具有多个嵌套布局的 Android 中处理多点触控事件?
【发布时间】:2016-02-14 22:59:48
【问题描述】:

我正在设计一个 Android 应用程序,我想找到一种方法来处理多个嵌套视图之间的多点触控。

我的布局如下:

Relative layout
|
|---Linear layout
|    \---Linear layout
|           \--Button A
|
|---Linear layout
     \---Button B

这两个按钮大约占屏幕大小的 50%。我希望能够同时一个接一个地按下两个按钮。我可以按 B 键,然后按 A,但是当我按 A 时,我无法按 B。

如果我们考虑 Android 处理事件的方式,这似乎是一种正常行为。我用谷歌搜索了我的问题,我只找到了涉及重写自定义视图和“DispatchTouchEvent”方法的复杂解决方案。

您知道是否有一种简单的方法可以避免这种行为(能够先按 A,然后按 B,然后按 B,然后按 A)?

我写了一个最小的例子:运行一个具有以下布局的活动

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:splitMotionEvents="true"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="epl.groupe16.testbutton.MainActivity">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:text="New Button"
        android:id="@+id/button" />

</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:weightSum="2">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button21"
            />
    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="New Button"
        android:id="@+id/button20"
        android:layout_weight="1"
        android:visibility="invisible" />
</LinearLayout>

我可以按最低的按钮,按住我的触摸然后点击上面的按钮,但如果我先尝试最高的按钮,最低的按钮是不可点击的。

提前谢谢你

【问题讨论】:

    标签: android layout multi-touch ontouch


    【解决方案1】:

    最后我们发现没有简单的解决方案。 但是如果有人遇到同样的问题,我想分享一些解决方法。

    • 首先,有一种手动方法可以做到这一点:通过创建自定义视图并覆盖“DispatchTouchEvent”方法来重新调整传播事件的正确方法。它很长,但它可以解决问题。

    • 此外,Google 在 API 23 中添加了 PercentRelativeLayout,因此您可以将所有组件放在一个视图中。但是,我们的目标是 API 19,Android Marshmallow 并不代表当前设备的很大一部分。

    • 最后,我们决定编写自定义 SurfaceView 和 touchEvent 监听器。我们在 SurfaceView 中绘制组件并手动处理 clics。

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多