onPopulateAccessibilityEvent

onPopulateAccessibilityEvent

本文介绍了在自定义视图上实现可访问性不提供口头反馈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已开启无障碍功能,当我四处导航时,我的设备会说话.

I have turned accessability on, and my device speaks as I navigate around.

我有一个自定义的 seekbar 并实现了以下内容:

I have a custom seekbar and have implemented the folllowing:

onTouchEvent 摘录:

...
case MotionEvent.ACTION_MOVE:

    getParent().requestDisallowInterceptTouchEvent(true);

    setTouchAngle(pointToAngle(touchX, touchY));
    score = getScoreFromAngle(angleStart,touchAngle);
    if (onScoreSetListener != null) {
        onScoreSetListener.onScorePoll(this, score);
        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
    }
    break;
...

onPopulateAccessibilityEvent 方法:

    @Override
    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
        super.onPopulateAccessibilityEvent(event);

        LogUtils.i(TAG,"onPopulateAccessibilityEvent()",null);

        switch (event.getEventType()) {
            case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:

                LogUtils.d(TAG,"dispatchPopulateAccessibilityEvent() TYPE_VIEW_TEXT_CHANGED",null);

                event.getText().add(String.valueOf(getScore()));
                break;
            }
    }

我可以看到在 LogCat 中成功调用了 onPopulateAccessibilityEvent,但设备没有给出任何反馈.我希望读回当前分数,但什么也没有.

I can see onPopulateAccessibilityEvent being called in LogCat successfully, but the device is not giving any feedback. I expect the current score to be read back, but nothing.

有没有人有任何见解?

推荐答案

如果您要扩展 ProgressBar,您可以设置传出 TYPE_VIEW_SELECTED 事件的文本.这些会在用户调整搜索栏时自动发送.

If you're extending ProgressBar, you can set the text for outgoing TYPE_VIEW_SELECTED events. These are sent automatically as the user adjusts the seek bar.

@Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
    super.onInitializeAccessibilityEvent(event);
    event.getText().add(...);
}

但是,您似乎扩展了View.在这种情况下,您需要使用一个轻微的解决方法并通过在 ICS 上发送 VIEW_FOCUSED 事件来触发通知,或者在 JellyBean 及更高版本上使用 announceForAccessibility API.这将需要 support-v4 库,如下所示:

However, it looks like you may have extended View. In this case, you will need to use a slight workaround and trigger an announcement by sending a VIEW_FOCUSED event on ICS, or use the announceForAccessibility API on JellyBean and above. Which would require the support-v4 library and would look like this:

/** The parent context. Used to obtain string resources. */
private final Context mContext;

/**
 * The accessibility manager for this context. This is used to check the
 * accessibility enabled state, as well as to send raw accessibility events.
 */
private final AccessibilityManager mA11yManager;

/**
 * Generates and dispatches an SDK-specific spoken announcement.
 * <p>
 * For backwards compatibility, we're constructing an event from scratch
 * using the appropriate event type. If your application only targets SDK
 * 16+, you can just call View.announceForAccessibility(CharSequence).
 * </p>
 *
 * @param text The text to announce.
 */
private void announceForAccessibilityCompat(CharSequence text) {
    if (!mA11yManager.isEnabled()) {
        return;
    }

    // Prior to SDK 16, announcements could only be made through FOCUSED
    // events. Jelly Bean (SDK 16) added support for speaking text verbatim
    // using the ANNOUNCEMENT event type.
    final int eventType;
    if (Build.VERSION.SDK_INT < 16) {
        eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED;
    } else {
        eventType = AccessibilityEventCompat.TYPE_ANNOUNCEMENT;
    }

    // Construct an accessibility event with the minimum recommended
    // attributes. An event without a class name or package may be dropped.
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.getText().add(text);
    event.setEnabled(isEnabled());
    event.setClassName(getClass().getName());
    event.setPackageName(mContext.getPackageName());

    // JellyBean MR1 requires a source view to set the window ID.
    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    record.setSource(this);

    // Sends the event directly through the accessibility manager. If your
    // application only targets SDK 14+, you should just call
    // getParent().requestSendAccessibilityEvent(this, event);
    mA11yManager.sendAccessibilityEvent(event);
}

这篇关于在自定义视图上实现可访问性不提供口头反馈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 00:00