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

时间:2012-10-08 07:50:37

标签: android android-layout android-intent android-emulator android-fragments

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

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

我尝试了以下方法:

方式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);
    }

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

3 个答案:

答案 0 :(得分: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>

顺便说一下。当你有2个linearlayout,每个布局有3个按钮时,你应该在这两个布局以及那个2个linearLayouts的父中设置这个splitMotionEvents。否则,每个布局只能单击一个按钮(sum = 2)。我希望你明白。 :)

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

答案 1 :(得分:11)

是的我曾经遇到过这个问题 我找到了 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>

我认为这对你有帮助。

答案 2 :(得分:1)

您可以在整个屏幕上放置一个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;
    }
}