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!
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