Disable multi finger touch in my app [duplicate]

2019-01-13 11:22发布

This question already has an answer here:

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.

Currently, user is able to press on two icons simultaneously with two fingers (with each fingure press on one icon). I want to disable this multi-touch feature on 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:

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

Way 2: In Android Manifest xml, I added:

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

Way 3: in my Activity:

@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 feature in my app??

3条回答
在下西门庆
2楼-- · 2019-01-13 11:31

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.

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

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

In Parent Layout only have two children A and 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

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

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.

2). just opposite of case 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.

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

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-13 11:35

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

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

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;
    }
}
查看更多
地球回转人心会变
4楼-- · 2019-01-13 11:56

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

android:splitMotionEvents="false"

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 parent of that 2 linearLayouts. Otherwise you will be able click only one button per layout (sum = 2 then). I hope you get it. :)

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

查看更多
登录 后发表回答