Skip to main content

AccessibilityInfo

有时,了解设备是否具有当前处于活动状态的屏幕阅读器很有用。AccessibilityInfo API 就是为此目的而设计的。你可以使用它来查询屏幕阅读器的当前状态,以及注册以在屏幕阅读器的状态发生变化时收到通知。

¥Sometimes it's useful to know whether or not the device has a screen reader that is currently active. The AccessibilityInfo API is designed for this purpose. You can use it to query the current state of the screen reader as well as to register to be notified when the state of the screen reader changes.

示例

¥Example


参考

¥Reference

方法

¥Methods

addEventListener()

static addEventListener(
eventName: AccessibilityChangeEventName | AccessibilityAnnouncementEventName,
handler: (
event: AccessibilityChangeEvent | AccessibilityAnnouncementFinishedEvent,
) => void,
): EmitterSubscription;

添加事件处理程序。支持的事件:

¥Add an event handler. Supported events:

事件名称描述
accessibilityServiceChanged
Android
当启用某些服务(例如 TalkBack、其他 Android 辅助技术和第三方辅助服务)时触发。事件处理程序的参数是一个布尔值。当启用某些辅助服务时,布尔值为 true,否则为 false
announcementFinished
iOS
当屏幕阅读器完成公告时触发。事件处理程序的参数是一个带有以下键的字典:
  • announcement:屏幕阅读器读出的字符串。
  • success:一个布尔值,指示公告是否成功发布。
boldTextChanged
iOS
当粗体文本切换的状态发生变化时触发。事件处理程序的参数是一个布尔值。当启用粗体文本时,布尔值为 true,否则为 false
grayscaleChanged
iOS
当灰度切换的状态发生变化时触发。事件处理程序的参数是一个布尔值。当灰度级启用时,布尔值为 true,否则为 false
invertColorsChanged
iOS
当反转颜色切换的状态发生变化时触发。事件处理程序的参数是一个布尔值。当启用反转颜色时,布尔值为 true,否则为 false
reduceMotionChanged当减少运动切换的状态发生变化时触发。事件处理程序的参数是一个布尔值。当启用减少运动时(或当 "开发者选项" 中的 "过渡动画比例" 为 "动画关闭" 时),布尔值为 true,否则为 false
reduceTransparencyChanged
iOS
当降低透明度切换的状态发生变化时触发。事件处理程序的参数是一个布尔值。当启用降低透明度时,布尔值为 true,否则为 false
screenReaderChanged当屏幕阅读器的状态更改时触发。事件处理程序的参数是一个布尔值。当屏幕阅读器启用时,布尔值为 true,否则为 false

announceForAccessibility()

static announceForAccessibility(announcement: string);

发布一个由屏幕阅读器读出的字符串。

¥Post a string to be announced by the screen reader.


announceForAccessibilityWithOptions()

static announceForAccessibilityWithOptions(
announcement: string,
options: options: {queue?: boolean},
);

发布一个由屏幕阅读器读出的字符串,并带有修改选项。默认情况下,公告会打断任何现有的语音,但在 iOS 上,可以通过在选项对象中将 queue 设置为 true,将它们排在现有语音后面。

¥Post a string to be announced by the screen reader with modification options. By default announcements will interrupt any existing speech, but on iOS they can be queued behind existing speech by setting queue to true in the options object.

参数:

¥Parameters:

名称类型描述
公告
必填
string待公布的字符串
选项
必填
objectqueue - 将公告排入现有语音
iOS

getRecommendedTimeoutMillis()
Android

static getRecommendedTimeoutMillis(originalTimeout: number): Promise<number>;

获取用户需要的超时(以毫秒为单位)。该值在 "采取行动的时间(辅助功能超时)" 或 "无障碍" 设置中设置。

¥Gets the timeout in millisecond that the user needs. This value is set in "Time to take action (Accessibility timeout)" of "Accessibility" settings.

参数:

¥Parameters:

名称类型描述
OriginalTimeout
必填
number如果未设置 "辅助功能超时",则返回超时。以毫秒为单位指定。

isAccessibilityServiceEnabled()
Android

static isAccessibilityServiceEnabled(): Promise<boolean>;

检查是否启用了任何无障碍服务。这包括 TalkBack,还包括可能安装的任何第三方辅助应用。要仅检查 TalkBack 是否启用,请使用 isScreenReaderEnabled。返回一个解析为布尔值的 promise。当启用某些辅助服务时,结果是 true,否则结果是 false

¥Check whether any accessibility service is enabled. This includes TalkBack but also any third-party accessibility app that may be installed. To only check whether TalkBack is enabled, use isScreenReaderEnabled. Returns a promise which resolves to a boolean. The result is true when some accessibility services is enabled and false otherwise.

注意:如果你只想查看 TalkBack 的状态,请使用 isScreenReaderEnabled

¥Note: Please use isScreenReaderEnabled if you only want to check the status of TalkBack.


isBoldTextEnabled()
iOS

static isBoldTextEnabled(): Promise<boolean>:

查询当前是否启用粗体文本。返回一个解析为布尔值的 promise。当启用粗体文本时,结果是 true,否则结果是 false

¥Query whether a bold text is currently enabled. Returns a promise which resolves to a boolean. The result is true when bold text is enabled and false otherwise.


isGrayscaleEnabled()
iOS

static isGrayscaleEnabled(): Promise<boolean>;

查询当前是否开启灰度。返回一个解析为布尔值的 promise。启用灰度时结果为 true,否则为 false

¥Query whether grayscale is currently enabled. Returns a promise which resolves to a boolean. The result is true when grayscale is enabled and false otherwise.


isInvertColorsEnabled()
iOS

static isInvertColorsEnabled(): Promise<boolean>;

查询当前是否启用反色。返回一个解析为布尔值的 promise。当启用反转颜色时,结果是 true,否则结果是 false

¥Query whether invert colors is currently enabled. Returns a promise which resolves to a boolean. The result is true when invert colors is enabled and false otherwise.


isReduceMotionEnabled()

static isReduceMotionEnabled(): Promise<boolean>;

查询当前是否启用了减少运动。返回一个解析为布尔值的 promise。当启用减少运动时,结果为 true,否则为 false

¥Query whether reduce motion is currently enabled. Returns a promise which resolves to a boolean. The result is true when reduce motion is enabled and false otherwise.


isReduceTransparencyEnabled()
iOS

static isReduceTransparencyEnabled(): Promise<boolean>;

查询当前是否开启降低透明度。返回一个解析为布尔值的 promise。当启用降低透明度时,结果是 true,否则结果是 false

¥Query whether reduce transparency is currently enabled. Returns a promise which resolves to a boolean. The result is true when a reduce transparency is enabled and false otherwise.


isScreenReaderEnabled()

static isScreenReaderEnabled(): Promise<boolean>;

查询当前是否启用屏幕阅读器。返回一个解析为布尔值的 promise。当屏幕阅读器启用时,结果是 true,否则结果是 false

¥Query whether a screen reader is currently enabled. Returns a promise which resolves to a boolean. The result is true when a screen reader is enabled and false otherwise.


prefersCrossFadeTransitions()
iOS

static prefersCrossFadeTransitions(): Promise<boolean>;

查询当前是否启用减少运动和首选交叉淡入淡出过渡设置。返回一个解析为布尔值的 promise。当启用首选交叉淡入淡出转换时,结果是 true,否则结果是 false

¥Query whether reduce motion and prefer cross-fade transitions settings are currently enabled. Returns a promise which resolves to a boolean. The result is true when prefer cross-fade transitions is enabled and false otherwise.


setAccessibilityFocus()

static setAccessibilityFocus(reactTag: number);

将可访问性焦点设置为 React 组件。

¥Set accessibility focus to a React component.

在 Android 上,这会调用 UIManager.sendAccessibilityEvent 方法并传递 reactTagUIManager.AccessibilityEventTypes.typeViewFocused 参数。

¥On Android, this calls UIManager.sendAccessibilityEvent method with passed reactTag and UIManager.AccessibilityEventTypes.typeViewFocused arguments.

注意:确保你想要接收辅助功能焦点的任何 View 都有 accessible={true}

¥Note: Make sure that any View you want to receive the accessibility focus has accessible={true}.