Disable or prevent multitouch in Activity

前端 未结 9 1684
闹比i
闹比i 2020-11-30 03:11

I have several Views on an Activity which a user wants to touch quickly in succession and I capture these touches using a TouchListener and handling Motio

相关标签:
9条回答
  • 2020-11-30 03:51

    Override dispatchTouchEvent and intercept all touches there, for all multi touches the pointercount is more than one.

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

    This cancels the touches and clears the pressed state of the buttons without taking any further action.

    0 讨论(0)
  • 2020-11-30 03:51

    For me it worked like this. I just put this line in all active styles in my app

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

    my style for example

    <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>
    
    0 讨论(0)
  • 2020-11-30 03:54

    I did it like this :

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

    I think you can override onTouchEvent for any view.

    0 讨论(0)
提交回复
热议问题