禁用或阻止“活动”中的多点触控

时间:2011-12-20 04:55:38

标签: android multi-touch

我有几个关于活动的观点,用户想要连续快速触摸,我使用TouchListener并处理MotionEvent.ACTION_DOWN捕获这些触摸。但是,如果用户使用双手,则在用户拉出上一根手指之前,下一个视图很可能会被“触摸”。在这种情况下,第一个视图会触发MotionEvent.ACTION_MOVE,而第二个视图则会触发所需的MotionEvent.ACTION_DOWN

有没有办法解决或阻止这种行为?我已尝试使用MotionEvent.ACTION_UP调度新事件并删除事件侦听器但似乎都不起作用。

9 个答案:

答案 0 :(得分:65)

我发现在整个应用中强制单次触摸的最简单方法是使用主题进行设置:

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:windowEnableSplitTouch">false</item>
    <item name="android:splitMotionEvents">false</item>
</style>

清单:

  <application
        android:label="@string/app_name"
        android:theme="@style/MyTheme" >

答案 1 :(得分:17)

如果有人仍在寻找最佳解决方案, 在xml中输入以下代码:

android:splitMotionEvents = false 

答案 2 :(得分:8)

我是这样做的:

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub

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

我认为您可以为任何视图覆盖onTouchEvent。

答案 3 :(得分:4)

解决此方案的最佳方法是使用ViewGroup并实施onInterceptTouchEvent方法,根据需要手动路由触摸事件。

这最初是在这里回答的: Android multitouch! hack anyone?

实现此解决方案的代码(可在上述问题的答案的评论部分找到)可在此处找到: http://pastebin.com/hiE1aTCw

答案 4 :(得分:2)

我使用自定义方法解决了这个问题 - 我不想这样做如果有人找到更好的方式我想听听它谢谢:

public static void setViewGroupEnebled(ViewGroup view, boolean enabled)
{
    int childern = view.getChildCount();

    for (int i = 0; i< childern ; i++)
    {
        View child = view.getChildAt(i);
        if (child instanceof ViewGroup)
        {
            setViewGroupEnebled((ViewGroup) child,enabled);
        }
        child.setEnabled(enabled);
    }
    view.setEnabled(enabled);
}

答案 5 :(得分:1)

覆盖dispatchTouchEvent并拦截那里的所有触摸,对于所有多次触摸,指针数量不止一个。

 if(ev.getPointerCount() > 1)
  {
     Log.d("Multitouch detected!");
     ev.setAction(MotionEvent.ACTION_CANCEL);
  }

取消触摸并清除按钮的按下状态,而不采取任何进一步操作。

答案 6 :(得分:0)

int currentapiVersion = android.os.Build.VERSION.SDK_INT;

            if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {

                horizentalLinearGridItem.setMotionEventSplittingEnabled(false);

            }

这里horizentalLinearGridItem是parentLayout视图,我们插入子视图。 在我的代码中它的LinearLayout。在这个LinearLayout中,我添加了子视图。但是当我同时点击两个子视图时,两者都得到了事件。阻止我使用上面的代码。

答案 7 :(得分:0)

我只需在OnTouchEvent方法中添加一些代码

就可以为我工作
boolean action_down_required = false;

@Override
public boolean onTouchEvent(MotionEvent event) {

    int action = event.getAction();

    if(event.getPointerCount() > 1){
        action_down_required = true;
        return true;
    }

    if(action_down_required){
        action = MotionEvent.ACTION_DOWN;
    }

    switch (action) {
        case MotionEvent.ACTION_DOWN:

            // your code goes here...

            action_down_required = false;
            break;

        // Other events...

答案 8 :(得分:0)

对我来说,它像这样工作。 我只是将这行代码以所有活跃的样式放在我的应用中

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

例如我的风格

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
      <item name="android:splitMotionEvents">false</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
      <item name="android:splitMotionEvents">false</item>
</style>