Implementing accessability on custom view gives no verbal feedback

前端 未结 1 1603
广开言路
广开言路 2021-01-01 00:51

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

I have a custom seekbar and have implemented the folllowing:

on

相关标签:
1条回答
  • 2021-01-01 01:40

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

    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);
    }
    
    0 讨论(0)
提交回复
热议问题