22

我的应用程序使用一个 Activity 来托管多个片段。每次在手机屏幕上显示一个片段。每个片段的视图由几个图像图标组成。

目前,用户可以用两根手指同时按下两个图标(每个手指按下一个图标)。我想在我的应用程序上禁用此多点触控功能,以允许一次仅按下一个图标生效

我尝试了以下方法:

方式1:在我的应用主题中,我添加了:

<item name="android:windowEnableSplitTouch">false</item>

方式2:在Android Manifest xml中,我添加了:

<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />

方式3:在我的活动中:

@Override
public boolean onTouchEvent(MotionEvent event) {

    if(event.getPointerCount() > 1) {
        System.out.println("Multitouch detected!");
        return true;
    }
    else
       return super.onTouchEvent(event);
    }

不幸的是,我的解决方案都不起作用。那么,如何在我的应用程序中禁用多点触控功能?

4

3 回答 3

41

例如:您有多个按钮,并且您只想选择一个按钮。按钮的父级(不是根,只是子级的父级)中,在其 xml 参数中添加

安卓:splitMotionEvents="假"

就是这样。例子:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:splitMotionEvents="false" <-----------!!!
    >

    <Button
    android:id="@+id/button_main_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button1" />

    <Button
    android:id="@+id/button_main_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button2" />

    <Button
    android:id="@+id/button_main_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button3" />
</LinearLayout>

顺便提一句。当您有 2 个线性布局,每个布局有 3 个按钮时,您应该在这两个布局中设置此 splitMotionEvents,并在这 2 个线性布局的父级中设置。否则,您将只能单击每个布局的一个按钮(然后总和 = 2)。我希望你能明白。:)

没有其他解决方案对我不起作用或太蹩脚。

于 2013-03-26T15:52:21.800 回答
14

是的,我也曾经遇到过这个问题,我发现android:splitMotionEvents="false"的解决方案 仅适用于引导布局的子级。

示例

就像您创建两个布局 A 和 B 并且在 A 中您有两个按钮 b1,b2 在 B 中您有两个按钮 b3,b4

A 和 B 都在父布局中所以首先你必须清楚这些按钮不是父布局的直接子元素

在父布局中只有两个孩子 A 和 B

如果您将android:splitMotionEvents="false"添加到父布局,那么多点触控将不适用于布局 A 和布局 B,但它将适用于每个按钮 b1、b2、b3、b4,因为它们不是父布局的直接子级

所以,如果你希望多点触控也不能在这些按钮上工作,那么你必须分别将android:splitMotionEvents="false"这个添加到每个布局中

有一些情况:

1). 如果您只在布局 A 上添加android:splitMotionEvents="false",则您不能同时触摸按钮 b1 和按钮 b2,但您可以同时触摸按钮 b1 或 b2 和按钮 b3 或 b4。

2)。与案例 1 正好相反。

3)。但是如果你在两个布局上都添加了android:splitMotionEvents="false",那么你就不能同时触摸屏幕上的任何两个按钮。

<LinearLayout 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:orientation="vertical"
android:splitMotionEvents="false" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:splitMotionEvents="false" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="b1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="b2" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:splitMotionEvents="false" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="b3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="b4" />
</LinearLayout>

我想它可能对你有帮助。

于 2014-03-25T13:00:05.420 回答
1

您可以将用户 a 放在GestureOverlayView整个屏幕上,并且只允许较低的视图处理第一个触摸事件。

您必须在这个透明视图上设置一个 onTouchListener,它执行如下操作:

gestureOverlayView.setOnTouchListener(new View.OnTouchListnener(){
    @Override
    public boolean onTouch(View v, MotionEvent e){
       // True means the event is ignored by the overlayed views 
       return e.getPointerCount() > 1 ? true : false;
    }
}
于 2012-10-08T08:06:45.280 回答