如何禁止 Scrollview 通过手写笔滚动?

How to disallow Scrollview scrolling through stylus?

我编写了自定义滚动视图,它在没有平板电脑手写笔的情况下工作正常。 使用平板电脑手写笔时,此 customScrollview 允许手写笔执行滚动,即使在禁用滚动时也是如此。

public class CustomScrollView extends ScrollView {

    private boolean enabled = false;

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean getEnabled() {
        return this.enabled;
    }
}

你可以试试这个。

 if (MotionEvent.getToolType(TOOL_TYPE_STYLUS))
 { 
 // disable scrollview 
 }

试试这个:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (ev.getToolType(TOOL_TYPE_STYLUS)) { 
        // also, here you should check if it should be disabled at this point
        return !enabled;
    } else {
        return enabled;
    }
}

重写 onInterceptHoverListener 成功了。 现在,如果启用了 scrollview,则处理手写笔悬停滚动,一旦启用 scrollview = false,则不会处理。

    public class CustomScrollView extends ScrollView {

        private boolean enabled = false;

        public CustomScrollView(Context context) {
            super(context);
        }

        public CustomScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

      @Override
        public boolean onInterceptHoverEvent(MotionEvent event) {
   if (this.enabled)
        {
            return false;
        }
        else
        {
          return true;
        }

}

       public boolean getEnabled() {
            return this.enabled;
        }
    }