Android 在我的应用程序中禁用多指触摸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12777435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:03:58  来源:igfitidea点击:

Disable multi finger touch in my app

androidandroid-layoutandroid-intentandroid-emulatorandroid-fragments

提问by Leem.fin

My app uses one Activity to host several fragments. Each time one fragment is shown on the phone screen.The view of each fragment consists of several image icons.

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

Currently, user is able to press on two icons simultaneouslywith two fingers (with each fingure press on one icon). I want to disable this multi-touch featureon my app to allow only one icon press take effect at a time.

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

I tried the following ways:

我尝试了以下方法:

Way 1:in my app theme, I added:

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

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

Way 2:In Android Manifest xml, I added:

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

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

Way 3:in my Activity:

方式3:在我的活动中:

@Override
public boolean onTouchEvent(MotionEvent event) {

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

Unfortunately, none of my solutions work. So, How can I disable multi-touch featurein my app??

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

回答by deadfish

For case: you have multiple buttons and you want make selected only one button. In parent(NOT ROOT, just parent of child) of buttons, in its xml params add

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

android:splitMotionEvents="false"

机器人:splitMotionEvents =“假”

And that's it. Example:

就是这样。例子:

<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>

Btw. When you have 2 linearlayout with 3 buttons per layout, you should set this splitMotionEvents in that two layouts and also in parentof that 2 linearLayouts. Otherwise you will be able click only one button per layout (sum = 2 then). I hope you get it. :)

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

None of the other solutions didn't work for me or was too lame.

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

回答by sachin pareek

Yes I also faced this issue once and I found solution that android:splitMotionEvents="false"this will work only to direct children of a layout.

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

Example:

示例

Like if you create two layout A and B and In A you have two buttons b1,b2 In B you have two buttons b3,b4

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

both A and B are in Parent Layout So first you have to clear that these buttons are not direct children of Parent Layout

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

In Parent Layout only have two children A and B

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

Than if you add android:splitMotionEvents="false"to Parent Layout then multi touch will not work on Layout A and Layout B but it will work on every button b1,b2,b3,b4 because these are not direct children of Parent Layout

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

So , If you want that multi touch will not work on these buttons also Than you have to add android:splitMotionEvents="false"this to every layout separately

因此,如果您希望多点触控也不适用于这些按钮,那么您必须将android:splitMotionEvents="false"分别添加到每个布局中

There are some cases :

有一些情况:

1).If you only add android:splitMotionEvents="false"on Layout A than you can not touch button b1 and button b2 at same time but you can touch button b1 or b2 and button b3 or b4 at same time.

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

2). just opposite of case 1.

2)。与情况 1 正好相反。

3). But if you add android:splitMotionEvents="false"on both layouts than you can not touch any two buttons on your screen at same time.

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>

I think it might helpful for you.

我想它可能对你有帮助。

回答by Emil Davtyan

You could put user a GestureOverlayViewaround the whole screen and only allow the first touch event to be processed by the lower views.

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

You must set up an onTouchListener on this transparent view, that does something like this:

您必须在此透明视图上设置一个 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;
    }
}