onTouchEvent is not called when talkback enabled on custom view

一笑奈何 提交于 2019-11-30 17:43:36

问题


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 selected value.

In my custom keyboard I need coordinates from MotionEvent so view can calculate on what draw (value) it was pressed.

But in this case when Talkback enabled onTouchEvent method is not called. It calls only when user double tap on view. Im trying to add custom OnTouchListener but it does not work. setFocusable=true and setFocusableInTouchMode=true.


回答1:


When TalkBack is enabled a double tap is the equivalent of a single tap. That is, onTouchEvent will only be called when the user double taps a view/widget.




回答2:


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


来源:https://stackoverflow.com/questions/26489714/ontouchevent-is-not-called-when-talkback-enabled-on-custom-view

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