# 辅助功能扩展上下文 AccessibilityExtensionContext是AccessibilityExtensionAbility上下文环境,继承自ExtensionContext。 辅助功能扩展上下文模块提供辅助功能扩展的上下文环境的能力,包括允许配置辅助应用关注信息类型、查询节点信息、手势注入等。 > **说明:** > > 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > 本模块接口仅可在Stage模型下使用。 ## 使用说明 在使用AccessibilityExtensionContext的功能前,需要通过AccessibilityExtensionAbility子类实例获取。 ```js import AccessibilityExtensionAbility from '@ohos.application.AccessibilityExtensionAbility' class MainAbility extends AccessibilityExtensionAbility { onConnect(): void { console.log('AxExtensionAbility onConnect'); let axContext = this.context; } } ``` ## FocusDirection 表示查询下一焦点元素的方向。 **系统能力**:以下各项对应的系统能力均为 SystemCapability.BarrierFree.Accessibility.Core | 名称 | 描述 | | -------- | ------- | | up | 表示向上查询。 | | down | 表示向上查询。 | | left | 表示向左查询。 | | right | 表示向右查询。 | | forward | 表示向前查询。 | | backward | 表示向后查询。 | ## FocusType 表示查询焦点元素的类型。 **系统能力**:以下各项对应的系统能力均为 SystemCapability.BarrierFree.Accessibility.Core | 名称 | 描述 | | ------------- | ----------- | | accessibility | 表示无障碍的焦点类型。 | | normal | 表示普通的焦点类型。 | ## Rect 表示矩形区域。 **系统能力**:以下各项对应的系统能力均为 SystemCapability.BarrierFree.Accessibility.Core | 名称 | 参数类型 | 可读 | 可写 | 说明 | | ------ | ------ | ---- | ---- | --------- | | left | number | 是 | 否 | 矩形区域的左边界。 | | top | number | 是 | 否 | 矩形区域的上边界。 | | width | number | 是 | 否 | 矩形区域的宽度。 | | height | number | 是 | 否 | 矩形区域的高度。 | ## WindowType 表示窗口的类型。 **系统能力**:以下各项对应的系统能力均为 SystemCapability.BarrierFree.Accessibility.Core | 名称 | 描述 | | ----------- | --------- | | application | 表示应用窗口类型。 | | system | 表示系统窗口类型。 | ## AccessibilityExtensionContext.setTargetBundleName setTargetBundleName(targetNames: Array\): Promise\; 设置关注的目标包名,使用Promise异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ------------------- | ---- | -------- | | targetNames | Array<string> | 是 | 关注的目标包名。 | **返回值:** | 类型 | 说明 | | ---------------------- | --------------------- | | Promise<void> | 无返回结果的Promise对象。 | **示例:** ```ts let targetNames = ['com.ohos.xyz']; this.context.setTargetBundleName().then(() => { console.info('set target bundle names success'); }).catch((err) => { console.error('failed to set target bundle names because ' + JSON.stringify(err)); }); ``` ## AccessibilityExtensionContext.setTargetBundleName setTargetBundleName(targetNames: Array\, callback: AsyncCallback\): void; 设置关注的目标包名,使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ------------------- | ---- | -------- | | targetNames | Array<string> | 是 | 关注的目标包名。 | | callback | AsyncCallback<void> | 是 | 回调函数,如果设置关注的目标包名失败,则AsyncCallback中err有数据返回。 | **示例:** ```ts let targetNames = ['com.ohos.xyz']; this.context.setTargetBundleName().then((err, data) => { if (err) { console.error('failed to set target bundle names because ' + JSON.stringify(err)); return; } console.info('set target bundle names success'); }); ``` ## AccessibilityExtensionContext.getFocusElement getFocusElement(isAccessibilityFocus?: boolean): Promise\; 获取焦点元素, 使用Promise异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | -------------------- | ------- | ---- | ------------------- | | isAccessibilityFocus | boolean | 否 | 获取的是否是无障碍焦点元素,默认为否。 | **返回值:** | 类型 | 说明 | | ----------------------------------- | ---------------------- | | Promise<AccessibilityElement> | Promise对象,返回当前对应的焦点元素。 | **示例:** ```ts let focusElement; this.context.getFocusElement().then((data) => { focusElement = data; console.log('get focus element success'); }).catch((err) => { console.error('failed to get focus element because ' + JSON.stringify(err)); }); ``` ## AccessibilityExtensionContext.getFocusElement getFocusElement(callback: AsyncCallback\): void; 获取焦点元素, 使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回当前对应的焦点元素。 | **示例:** ```ts let focusElement; this.context.getFocusElement().then((err, data) => { if (err) { console.error('failed to get focus element because ' + JSON.stringify(err)); return; } focusElement = data; console.info('get focus element success'); }); ``` ## AccessibilityExtensionContext.getFocusElement getFocusElement(isAccessibilityFocus: boolean, callback: AsyncCallback\): void; 获取焦点元素, 使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | -------------------- | ------- | ---- | ------------------- | | isAccessibilityFocus | boolean | 是 | 获取的是否是无障碍焦点元素。 | | callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回当前对应的焦点元素。 | **示例:** ```ts let isAccessibilityFocus = true; this.context.getFocusElement(isAccessibilityFocus).then((err, data) => { if (err) { console.error('failed to get focus element because ' + JSON.stringify(err)); return; } focusElement = data; console.info('get focus element success'); }); ``` ## AccessibilityExtensionContext.getWindowRootElement getWindowRootElement(windowId?: number): Promise\; 获取指定窗口的根节点元素, 使用Promise异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | -------------------- | ------- | ---- | ------------------- | | windowId | number | 否 | 指定窗口的编号,未指定则从当前活跃窗口获取。 | **返回值:** | 类型 | 说明 | | ----------------------------------- | ---------------------- | | Promise<AccessibilityElement> | Promise对象,返回指定屏幕的所有窗口。 | **示例:** ```ts let rootElement; this.context.getWindowRootElement().then((data) => { rootElement = data; console.log('get root element of the window success'); }).catch((err) => { console.error('failed to get root element of the window because ' + JSON.stringify(err)); }); ``` ## AccessibilityExtensionContext.getWindowRootElement getWindowRootElement(callback: AsyncCallback\): void; 获取指定窗口的根节点元素, 使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回指定窗口的根节点元素。 | **示例:** ```ts let rootElement; this.context.getWindowRootElement().then((err, data) => { if (err) { console.error('failed to get root element of the window because ' + JSON.stringify(err)); return; } rootElement = data; console.info('get root element of the window success'); }); ``` ## AccessibilityExtensionContext.getWindowRootElement getWindowRootElement(windowId: number, callback: AsyncCallback\): void; 获取指定屏幕中的所有窗口, 使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | -------------------- | ------- | ---- | ------------------- | | windowId | number | 是 | 指定窗口的编号,未指定则从当前活跃窗口获取。 | | callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回指定窗口的根节点元素。 | **示例:** ```ts let displayId = 10; let rootElement; this.context.getWindowRootElement(displayId).then((err, data) => { if (err) { console.error('failed to get root element of the window because ' + JSON.stringify(err)); return; } rootElement = data; console.info('get root element of the window success'); }); ``` ## AccessibilityExtensionContext.getWindows getWindows(displayId?: number): Promise\>; 获取指定屏幕中的所有窗口, 使用Promise异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | -------------------- | ------- | ---- | ------------------- | | displayId | number | 否 | 指定的屏幕编号,未指定则从默认主屏幕获取。 | **返回值:** | 类型 | 说明 | | ----------------------------------- | ---------------------- | | Promise<Array<AccessibilityElement>> | Promise对象,返回指定屏幕的所有窗口。 | **示例:** ```ts let windows; this.context.getWindows().then((data) => { windows = data; console.log('get windows success'); }).catch((err) => { console.error('failed to get windows because ' + JSON.stringify(err)); }); ``` ## AccessibilityExtensionContext.getWindows getWindows(callback: AsyncCallback\>): void; 获取指定屏幕中的所有窗口, 使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | callback | AsyncCallback<Array<AccessibilityElement>> | 是 | 回调函数,返回指定屏幕的所有窗口。 | **示例:** ```ts let windows; this.context.getWindows().then((err, data) => { if (err) { console.error('failed to get windows because ' + JSON.stringify(err)); return; } windows = data; console.info('get windows success'); }); ``` ## AccessibilityExtensionContext.getWindows getWindows(displayId: number, callback: AsyncCallback\>): void; 获取指定屏幕中的所有窗口, 使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | -------------------- | ------- | ---- | ------------------- | | displayId | number | 是 | 指定的屏幕编号,未指定则从默认主屏幕获取。 | | callback | AsyncCallback<Array<AccessibilityElement>> | 是 | 回调函数,返回指定屏幕的所有窗口。 | **示例:** ```ts let displayId = 10; let windows; this.context.getWindows(displayId).then((err, data) => { if (err) { console.error('failed to get windows because ' + JSON.stringify(err)); return; } windows = data; console.info('get windows success'); }); ``` ## AccessibilityExtensionContext.injectGesture injectGesture(gesturePath: GesturePath): Promise\; 注入手势,使用Promise异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | gesturePath | [GesturePath](js-apis-accessibility-GesturePath.md#gesturepath) | 是 | 表示手势的路径信息。 | **返回值:** | 类型 | 说明 | | ----------------------------------- | ---------------------- | | Promise<void> | 无返回结果的Promise对象。 | **示例:** ```ts import { GesturePath } from "../@ohos.accessibility.GesturePath"; let gesturePath = new GesturePath(100); for (let i = 0; i < 10; i++) { let gesturePoint = new GesturePosition(100, i * 200); gesturePath.positions.push(gesturePoint); } this.context.gestureInject(gesturePath, () => { console.info('inject gesture success'); }).catch((err) => { console.error('failed to inject gesture because ' + JSON.stringify(err)); }); ``` ## AccessibilityExtensionContext.injectGesture injectGesture(gesturePath: GesturePath, callback: AsyncCallback\): void 注入手势,使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | gesturePath | [GesturePath](js-apis-accessibility-GesturePath.md#gesturepath) | 是 | 表示手势的路径信息。 | | callback | AsyncCallback<void> | 是 | 回调函数,表示注入手势执行结果的回调。 | **示例:** ```ts let gesturePath = new GesturePath(100); for (let i = 0; i < 10; i++) { let gesturePoint = new GesturePosition(100, i * 200); gesturePath.positions.push(gesturePoint); } this.context.gestureInject(gesturePath, (err, data) => { if (err) { console.error('failed to inject gesture because ' + JSON.stringify(err)); return; } console.info('inject gesture success'); }); ``` ## AccessibilityElement9 无障碍节点元素。 **系统能力**:以下各项对应的系统能力均为SystemCapability.BarrierFree.Accessibility.Core ## attributeNames attributeNames\(): Promise\>; 获取节点元素的所有属性名称,使用Promise异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------------ | | Promise<Array<T>> | Promise对象,返回节点元素的所有属性名称。 | **示例:** ```ts let accessibilityElement; let attributeNames; try { accessibilityElement.attributeNames().then((data) => { console.log('get attribute names success'); attributeNames = data; }).catch((err) => { console.log('get attribute names err: ' + JSON.stringify(err)); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## attributeNames attributeNames\(callback: AsyncCallback\>): void; 获取节点元素的所有属性名称,使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | callback | AsyncCallback<Array<T>> | 是 | 回调函数,返回节点元素的所有属性名称。 | **示例:** ```ts let accessibilityElement; let attributeNames; try { accessibilityElement.attributeNames().then((err, data) => { if (err) { console.error('failed to get attribute names because ' + JSON.stringify(err)); return; } attributeNames = data; console.info('get attribute names success'); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## AccessibilityElement.attributeValue attributeValue\(attributeName: T): Promise\; 根据属性名称获取属性值,使用Promise异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | attributeName | T | 是 | 表示属性的名称。 | **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------------ | | Promise<ElementAttributeValues[T]> | Promise对象,返回根据节点属性名称获取的属性值。 | **示例:** ```ts let accessibilityElement; let attributeValue; try { let attributeName = 'name'; accessibilityElement.attributeValue(attributeName).then((data) => { console.log('get attribute value by name success'); attribtueValue = data; }).catch((err) => { console.log('get attribute value by name err: ' + JSON.stringify(err)); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## AccessibilityElement.attributeValue attributeValue\(attributeName: T, callback: AsyncCallback\): void; 根据属性名称获取属性值,使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | attributeName | T | 是 | 表示属性的名称。 | | callback | AsyncCallback<ElementAttributeValues[T]> | 是 | 回调函数,返回根据节点属性名称获取的属性值。 | **示例:** ```ts let accessibilityElement; let attributeValue; try { let attributeName = 'name'; accessibilityElement.attributeValue(attributeName).then((err, data) => { if (err) { console.error('failed to get attribute value because ' + JSON.stringify(err)); return; } attributeValue = data; console.info('get attribute value success'); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## actionNames actionNames(): Promise\>; 获取节点元素支持的所有操作名称,使用Promise异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------------ | | Promise<Array<string>> | Promise对象,返回节点元素支持的所有操作名称。 | **示例:** ```ts let accessibilityElement; let actionNames; try { accessibilityElement.actionNames().then((data) => { console.log('get action names success'); actionNames = data; }).catch((err) => { console.log('get action names err: ' + JSON.stringify(err)); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## actionNames actionNames(callback: AsyncCallback\>): void; 获取节点元素支持的所有操作名称,使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | callback | AsyncCallback<Array<string>> | 是 | 回调函数,返回节点元素支持的所有操作名称。 | **示例:** ```ts let accessibilityElement; let actionNames; try { accessibilityElement.actionNames().then((err, data) => { if (err) { console.error('failed to get action names because ' + JSON.stringify(err)); return; } actionNames = data; console.info('get action names success'); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## performAction performAction(actionName: string, parameters?: object): Promise\; 根据操作名称执行某个操作,使用Promise异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | actionName | string | 是 | 表示属性的名称。 | | parameters | object | 否 | 表示执行操作时所需要的参数。 | **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------------ | | Promise<boolean> | Promise对象,返回执行指定操作后的回调结果,true为执行成功,false为执行失败。 | **示例:** ```ts let accessibilityElement; let performActionRes; try { accessibilityElement.performAction('action').then((data) => { console.info('perform action success'); performActionRes = data; }).catch((err) => { console.log('failed to perform action because ' + JSON.stringify(err)); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## performAction performAction(actionName: string, callback: AsyncCallback\): void; 根据操作名称执行某个操作,使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | actionName | string | 是 | 表示属性的名称。 | | callback | AsyncCallback<boolean> | 是 | 回调函数,返回执行指定操作后的回调结果,true为执行成功,false为执行失败。 | **示例:** ```ts let accessibilityElement; let performActionRes; try { accessibilityElement.performAction('action').then((err, data) => { if (err) { console.error('failed to perform action because ' + JSON.stringify(err)); return; } performActionRes = data; console.info('perform action success'); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## performAction performAction(actionName: string, parameters: object, callback: AsyncCallback\): void; 根据操作名称执行某个操作,使用callback异步回调。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | actionName | string | 是 | 表示属性的名称。 | | parameters | object | 是 | 表示执行操作时所需要的参数。 | | callback | AsyncCallback<boolean> | 是 | 回调函数,返回执行指定操作后的回调结果,true为执行成功,false为执行失败。 | **示例:** ```ts let accessibilityElement; let actionName = 'action'; let parameters = { 'setText': 'test text' }; let performActionRes; try { accessibilityElement.performAction(actionName, parameters).then((err, data) => { if (err) { console.error('failed to perform action because ' + JSON.stringify(err)); return; } performActionRes = data; console.info('perform action success'); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## findElement('content') findElement(type: 'content', condition: string): Promise\>; 根据节点内容查询所有节点元素。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | type | string | 是 | 固定为'content', 表示查找的类型为节点元素内容。 | | condition | string | 是 | 表示查找的条件。 | **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------------ | | Promise<Array<AccessibilityElement>> | Promise对象,返回满足指定查询关键字的所有节点元素。 | **示例:** ```ts let accessibilityElement; let type = 'content'; let condition = 'keyword'; let elements; try { accessibilityElement.findElement(type, condition).then((data) => { elements = data; console.log('find element success'); }).catch((err) => { console.log('failed to find element because ' + JSON.stringify(err)); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## findElement('content') findElement(type: 'content', condition: string, callback: AsyncCallback\>): void; 根据节点内容查询所有节点元素。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | type | string | 是 | 固定为'content',表示查找的类型为节点元素内容。 | | condition | string | 是 | 表示查找的条件。 | | callback | AsyncCallback<Array<AccessibilityElement>> | 是 | 回调函数,返回满足指定查询关键字的所有节点元素。 | **示例:** ```ts let accessibilityElement; let type = 'content'; let condition = 'keyword'; let elements; try { accessibilityElement.findElement(type, condition).then((err, data) => { if (err) { console.error('failed to find element because ' + JSON.stringify(err)); return; } elements = data; console.info('find element success'); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## findElement('focusType') findElement(type: 'focusType', condition: FocusType): Promise\; 根据焦点元素类型查询节点元素。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | type | string | 是 | 固定为'focusType', 表示查询的类型为节点的焦点元素类型。 | | condition | [FocusType](#focustype) | 是 | 表示查询焦点元素的类型。 | **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------------ | | Promise<AccessibilityElement> | Promise对象,返回满足指定查询焦点元素类型的节点元素。 | **示例:** ```ts let accessibilityElement; let type = 'focusType'; let condition = 'normal'; let elements; try { accessibilityElement.findElement(type, condition).then((data) => { elements = data; console.log('find element success'); }).catch((err) => { console.log('failed to find element because ' + JSON.stringify(err)); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## findElement('focusType') findElement(type: 'focusType', condition: FocusType, callback: AsyncCallback\): void; 根据焦点元素类型查询节点元素。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | type | string | 是 | 固定为'focusType', 表示查询的类型为节点的焦点元素类型。 | | condition | [FocusType](#focustype) | 是 | 表示查询焦点元素的类型。 | | callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回满足指定查询焦点元素类型的节点元素。 | **示例:** ```ts let accessibilityElement; let type = 'focusType'; let condition = 'normal'; let elements; try { accessibilityElement.findElement(type, condition).then((err, data) => { if (err) { console.error('failed to find element because ' + JSON.stringify(err)); return; } elements = data; console.info('find element success'); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## findElement('focusDirection') findElement(type: 'focusDirection', condition: FocusDirection): Promise\; 根据下一焦点元素方向查询节点元素。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | type | string | 是 | 固定为'focusDirection', 表示查询的类型为节点的下一焦点元素方向。 | | condition | [FocusDirection](#focusdirection) | 是 | 表示查询下一焦点元素的方向。 | **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------------ | | Promise<AccessibilityElement> | Promise对象,返回满足指定查询下一焦点元素方向的节点元素。 | **示例:** ```ts let accessibilityElement; let type = 'focusDirection'; let condition = 'up'; let elements; try { accessibilityElement.findElement(type, condition).then((data) => { elements = data; console.log('find element success'); }).catch((err) => { console.log('failed to find element because ' + JSON.stringify(err)); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ``` ## findElement('focusDirection') findElement(type: 'focusDirection', condition: FocusDirection, callback: AsyncCallback\): void; 根据下一焦点元素方向查询所有节点元素。 **系统能力:** SystemCapability.BarrierFree.Accessibility.Core **参数:** | 参数名 | 参数类型 | 必填 | 说明 | | ----------- | ---------------------------------------- | ---- | -------------- | | type | string | 是 | 固定为'focusDirection', 表示查询的类型为节点的下一焦点元素方向。 | | condition | [FocusDirection](#focusdirection) | 是 | 表示下一查询焦点元素的方向。 | | callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回满足指定查询下一焦点元素方向的节点元素。 | **示例:** ```ts let accessibilityElement; let type = 'focusDirection'; let condition = 'up'; let elements; try { accessibilityElement.findElement(type, condition).then((err, data) => { if (err) { console.error('failed to find element because ' + JSON.stringify(err)); return; } elements = data; console.info('find element success'); }); } catch (e) { console.log('An unexpected error occurred. Error:' + e); } ```