Disable or prevent multitouch in Activity

前端 未结 9 1683
闹比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:30

    The best way to work around this scenario is to use a ViewGroup and implement the onInterceptTouchEvent method to manually route the touch events as you see fit.

    This was originally answered here: Android multitouch! hack anyone?

    Code implementing this solution (found in the comments section of the answer to the above question) is found here: http://pastebin.com/hiE1aTCw

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

    The easiest way I found to force single touch across an entire app is to set it using a theme:

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

    Manifest:

      <application
            android:label="@string/app_name"
            android:theme="@style/MyTheme" >
    
    0 讨论(0)
  • 2020-11-30 03:37

    I have solved this using a custom method - which I did not want to do If anyone finds a better way I'd like to hear about it Thanks:

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

    if someone still searching for best solution, put in the xml the following code :

    android:splitMotionEvents = false 
    
    0 讨论(0)
  • 2020-11-30 03:43

    I got it to work for me by just adding some code in OnTouchEvent method,

    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...
    
    0 讨论(0)
  • 2020-11-30 03:46
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    
                if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
    
                    horizentalLinearGridItem.setMotionEventSplittingEnabled(false);
    
                }
    

    Here horizentalLinearGridItem is parentLayout view where we insert child views. In my code its LinearLayout. In this LinearLayout I added child views. But when I clicked two child views simultaneously, both were getting the events. To block that I used the above code.

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