在我的应用中禁用多指触摸

Lee*_*fin 19 android android-emulator android-intent android-layout android-fragments

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

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

我尝试了以下方法:

方式1:在我的应用主题中,我补充说:

<item name="android:windowEnableSplitTouch">false</item>
Run Code Online (Sandbox Code Playgroud)

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

<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
Run Code Online (Sandbox Code Playgroud)

方式3:在我的活动中:

@Override
public boolean onTouchEvent(MotionEvent event) {

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

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

dea*_*ish 37

例如:你有多个按钮,你只想选择一个按钮.按钮的(NOT ROOT,只是孩子的父)中,在其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>
Run Code Online (Sandbox Code Playgroud)

顺便说一句.当你有2 LinearLayout中,每个布局3个按钮,则应该在这两个布局这splitMotionEvents 并在父母那2个linearLayouts的.否则,每个布局只能单击一个按钮(sum = 2).我希望你明白.:)

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


sac*_*eek 11

是的我曾经遇到过这个问题,我找到了android:splitMotionEvents ="false"的解决方案, 这只能用于指导布局的子项.

示例:

就像你创建了两个布局A和B而在A中你有两个按钮b1,b2在B中你有两个按钮b3,b4

A和B都在父布局中首先,您必须清除这些按钮不是父布局的直接子节点

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

如果你将android:splitMotionEvents ="false"添加到Parent Layout,那么多点触摸将无法在布局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>
Run Code Online (Sandbox Code Playgroud)

我想这对你有帮助.


Emi*_*yan 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;
    }
}
Run Code Online (Sandbox Code Playgroud)