【问题标题】:Disable or prevent multitouch in Activity在 Activity 中禁用或阻止多点触控
【发布时间】:2012-01-24 03:40:16
【问题描述】:

我有几个用户想要连续快速触摸的 Activity 视图,我使用 TouchListener 捕获这些触摸并处理 MotionEvent.ACTION_DOWN。但是,如果用户使用两只手,则很可能在用户拉起前一个手指之前,下一个视图将被“触摸”。在这种情况下,第一个视图会触发 MotionEvent.ACTION_MOVE,而不是第二个视图所需的 MotionEvent.ACTION_DOWN

有没有办法解决或防止这种行为?我尝试使用 MotionEvent.ACTION_UP 发送一个新事件并删除事件侦听器,但似乎都不起作用。

【问题讨论】:

    标签: android multi-touch


    【解决方案1】:

    对我来说,它是这样工作的。 我只是把这条线放在我的应用程序中的所有活动样式中

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

    【讨论】:

      【解决方案2】:

      重写 dispatchTouchEvent 并拦截那里的所有触摸,对于所有多点触摸,指针计数大于一。

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

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

      【讨论】:

      • 第一次工作,第二次我做多点触控然后我单点触控它没有被检测到。
      【解决方案3】:

      我只需在 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...
      

      【讨论】:

        【解决方案4】:

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

        android:splitMotionEvents = false 
        

        【讨论】:

        • 它应该放在布局中。
        • 是的,它有效!将其插入您需要禁用多点触控的布局中。
        • 在父布局中使用它会完成工作
        • 在具有可点击项目的布局中使用它,不要将它放在最顶层的布局/根视图中。
        【解决方案5】:
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        
                    if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        
                        horizentalLinearGridItem.setMotionEventSplittingEnabled(false);
        
                    }
        

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

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

          • 仅供参考,“android:splitMotionEvents”需要 API 级别 11 及以上
          • 在 Android 5.0.1 中为我工作。
          • 即使在 Android 8 上也能正常工作。
          【解决方案7】:

          我是这样做的:

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

          【讨论】:

            【解决方案8】:

            解决这种情况的最佳方法是使用ViewGroup 并实现onInterceptTouchEvent 方法来手动路由您认为合适的触摸事件。

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

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

            【讨论】:

              【解决方案9】:

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2012-08-20
                • 2010-11-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-01-02
                相关资源
                最近更新 更多