Android - Override TalkBack gestures

别来无恙 提交于 2019-12-07 15:24:23

问题


I want to use TalkBack within my app, but still want some activities to behave differently. For example, when entering a specific activity I want to select a button (trigger a button click) when lifting finger up from that button. TalkBack enables only double-click to select a button.

How can I "override" TalkBack gestures?

Thanks!


回答1:


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


来源:https://stackoverflow.com/questions/17018252/android-override-talkback-gestures

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