onTouchEvent is not called when talkback enabled on custom view

前端 未结 2 1281
臣服心动
臣服心动 2021-01-06 04:03

I\'m implementing custom keyboard (through custom view) for password field and trying add accessibility feature, so when user single press on view it should pronounce select

2条回答
  •  抹茶落季
    2021-01-06 04:08

    For those that come across this question and looking for a solution; When accessibility (Talkback) is enabled, onTouchEvent method is not called on single tap, it's called on double taps instead.

    To catch single taps when accessibility is enabled, and/or override this behaviour, onHoverEvent method of View class should be overriden. By using this method, you can catch single touch down as ACTION_HOVER_ENTER, move as ACTION_HOVER_MOVE and up as ACTION_HOVER_EXIT.

    Also you can override this behaviour by modifying the action of caught MotionEvent and sending it to onTouchEvent method as shown below:

    @Override
    public boolean onHoverEvent(MotionEvent event) {
        if (accessibilityManager.isTouchExplorationEnabled() && event.getPointerCount() == 1) {
            final int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_HOVER_ENTER: {
                    event.setAction(MotionEvent.ACTION_DOWN);
                } break;
                case MotionEvent.ACTION_HOVER_MOVE: {
                    event.setAction(MotionEvent.ACTION_MOVE);
                } break;
                case MotionEvent.ACTION_HOVER_EXIT: {
                    event.setAction(MotionEvent.ACTION_UP);
                } break;
            }
            return onTouchEvent(event);
        }
        return true;
    }
    

提交回复
热议问题