在自定义视图上实现辅助功能不会给出口头反馈

时间:2013-07-25 15:00:25

标签: android accessibility android-custom-view

我已经开启了可访问性,当我浏览时,我的设备会说话。

我有一个自定义seekbar并已实施以下内容:

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

我可以看到onPopulateAccessibilityEvent成功调用LogCat,但该设备未提供任何反馈。我希望现在的分数可以回读,但没有。

有没有人有任何见解?

1 个答案:

答案 0 :(得分:17)

如果您要延长ProgressBar,则可以为传出的TYPE_VIEW_SELECTED事件设置文本。这些是在用户调整搜索栏时自动发送的。

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

但是,您似乎已经延长了View。在这种情况下,您需要使用轻微的解决方法并通过在ICS上发送VIEW_FOCUSED事件来触发通知,或者在JellyBean及更高版本上使用announceForAccessibility API。哪个需要support-v4库,看起来像这样:

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