Set contentDescription for a custom dialog

2019-04-29 16:17发布

问题:

how can I set a contentDescription to be read by the accessibility service when my dialog is opened? I'm currently using a dialog with a custom layout to display a blocking laoding screen and the spoken contentDescription is "Alert", which is not appropriate in this circumstance.

Thanks ;)

回答1:

You can create subclass Dialog and override dispatchPopulateAccessibilityEvent to provide your own accessibility text.

Here's an example:

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
        event.getText().add("Your message goes here");
        return true;
    }
    return super.dispatchPopulateAccessibilityEvent(event);
}