【发布时间】: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