Android - Override TalkBack gestures

折月煮酒 提交于 2019-12-05 22:16:44

You can perform a click action on HOVER_EXIT, but you'll need to do some work to prevent TalkBack from expecting the normal double-click action. The phone dialer's DialPadImageButton provides a good example of this behavior. Here are some relevant portions of code from that class:

@Override
public boolean onHoverEvent(MotionEvent event) {
    // When touch exploration is turned on, lifting a finger while inside
    // the button's hover target bounds should perform a click action.
    if (mAccessibilityManager.isEnabled()
            && mAccessibilityManager.isTouchExplorationEnabled()) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_HOVER_ENTER:
                // Lift-to-type temporarily disables double-tap activation.
                setClickable(false);
                break;
            case MotionEvent.ACTION_HOVER_EXIT:
                if (mHoverBounds.contains((int) event.getX(), (int) event.getY())) {
                    simulateClickForAccessibility();
                }
                setClickable(true);
                break;
        }
    }

    return super.onHoverEvent(event);
}

/**
 * When accessibility is on, simulate press and release to preserve the
 * semantic meaning of performClick(). Required for Braille support.
 */
private void simulateClickForAccessibility() {
    // Checking the press state prevents double activation.
    if (isPressed()) {
        return;
    }

    setPressed(true);

    // Stay consistent with performClick() by sending the event after
    // setting the pressed state but before performing the action.
    sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);

    setPressed(false);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!