W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
id為組件的唯一標識,在整個應用內唯一。本模塊提供組件標識相關接口,可以獲取指定id組件的屬性,也提供向指定id組件發(fā)送事件的功能。
從API Version 8開始支持。后續(xù)版本如有新增內容,則采用上角標單獨標記該內容的起始版本。
getInspectorByKey(id: string): string
獲取指定id的組件的所有屬性,不包括子組件信息。
此接口僅用于對應用的測試。
參數:
參數 | 類型 | 必填 | 描述 |
---|---|---|---|
id | string | 是 | 要獲取屬性的組件id。 |
返回值:
類型 | 描述 |
---|---|
string | 組件屬性列表的JSON字符串。 |
getInspectorTree(): Object
獲取組件樹及組件屬性。
此接口僅用于對應用的測試。
返回值:
類型 | 描述 |
---|---|
Object | 組件樹及組件屬性列表的JSON對象。 |
sendEventByKey(id: string, action: number, params: string): boolean
給指定id的組件發(fā)送事件。
此接口僅用于對應用的測試。
參數:
參數 | 類型 | 必填 | 描述 |
---|---|---|---|
id | string | 是 | 要觸發(fā)事件的組件的id。 |
action | number | 是 | 要觸發(fā)的事件類型,目前支持取值: - 點擊事件Click: 10 - 長按事件LongClick: 11。 |
params | string | 是 | 事件參數,無參數傳空字符串 ""。 |
返回值:
類型 | 描述 |
---|---|
boolean | 找不到指定id的組件時返回false,其余情況返回true。 |
sendTouchEvent(event: TouchObject): boolean
發(fā)送觸摸事件。
此接口僅用于對應用的測試。
參數:
參數 | 類型 | 必填 | 描述 |
---|---|---|---|
event | TouchObject | 是 | 觸發(fā)觸摸事件的位置,event參數見TouchEvent中TouchObject的介紹。 |
返回值:
類型 | 描述 |
---|---|
boolean | 事件發(fā)送失敗時返回false,其余情況返回true。 |
sendKeyEvent(event: KeyEvent): boolean
發(fā)送按鍵事件。
此接口僅用于對應用的測試。
參數:
返回值:
類型 | 描述 |
---|---|
boolean | 事件發(fā)送失敗時時返回false,其余情況返回true。 |
sendMouseEvent(event: MouseEvent): boolean
發(fā)送鼠標事件。
此接口僅用于對應用的測試。
參數:
參數 | 類型 | 必填 | 描述 |
---|---|---|---|
event | MouseEvent | 是 | 鼠標事件,event參數見MouseEvent介紹。 |
返回值:
類型 | 描述 |
---|---|
boolean | 事件發(fā)送失敗時返回false,其余情況返回true。 |
- // xxx.ets
- class Utils {
- static rect_left
- static rect_top
- static rect_right
- static rect_bottom
- static rect_value
- //獲取組件所占矩形區(qū)域坐標
- static getComponentRect(key) {
- let strJson = getInspectorByKey(key)
- let obj = JSON.parse(strJson)
- console.info("[getInspectorByKey] current component obj is: " + JSON.stringify(obj))
- let rectInfo = JSON.parse('[' + obj.$rect + ']')
- console.info("[getInspectorByKey] rectInfo is: " + rectInfo)
- this.rect_left = JSON.parse('[' + rectInfo[0] + ']')[0]
- this.rect_top = JSON.parse('[' + rectInfo[0] + ']')[1]
- this.rect_right = JSON.parse('[' + rectInfo[1] + ']')[0]
- this.rect_bottom = JSON.parse('[' + rectInfo[1] + ']')[1]
- return this.rect_value = {
- "left": this.rect_left, "top": this.rect_top, "right": this.rect_right, "bottom": this.rect_bottom
- }
- }
- }
- @Entry
- @Component
- struct IdExample {
- @State text: string = ''
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
- Button() {
- Text('onKeyTab').fontSize(25).fontWeight(FontWeight.Bold)
- }.margin({ top: 20 }).backgroundColor('#0D9FFB')
- .onKeyEvent(() => {
- this.text = "onKeyTab"
- })
- Button() {
- Text('click to start').fontSize(25).fontWeight(FontWeight.Bold)
- }.margin({ top: 20 })
- .onClick(() => {
- console.info(getInspectorByKey("click"))
- console.info(JSON.stringify(getInspectorTree()))
- this.text = "Button 'click to start' is clicked"
- setTimeout(() => {
- sendEventByKey("longClick", 11, "") // 向id為"longClick"的組件發(fā)送長按事件
- }, 2000)
- }).id('click')
- Button() {
- Text('longClick').fontSize(25).fontWeight(FontWeight.Bold)
- }.margin({ top: 20 }).backgroundColor('#0D9FFB')
- .gesture(
- LongPressGesture().onActionEnd(() => {
- console.info('long clicked')
- this.text = "Button 'longClick' is longclicked"
- setTimeout(() => {
- let rect = Utils.getComponentRect('onTouch') // 獲取id為"onTouch"組件的矩形區(qū)域坐標
- let touchPoint: TouchObject = {
- id: 1,
- x: rect.left + (rect.right - rect.left) / 2, // 組件中心點x坐標
- y: rect.top + (rect.bottom - rect.top) / 2, // 組件中心點y坐標
- type: TouchType.Down,
- screenX: rect.left + (rect.right - rect.left) / 2, // 組件中心點x坐標
- screenY: rect.left + (rect.right - rect.left) / 2, // 組件中心點y坐標
- }
- sendTouchEvent(touchPoint) // 發(fā)送觸摸事件
- touchPoint.type = TouchType.Up
- sendTouchEvent(touchPoint) // 發(fā)送觸摸事件
- }, 2000)
- })).id('longClick')
- Button() {
- Text('onTouch').fontSize(25).fontWeight(FontWeight.Bold)
- }.type(ButtonType.Capsule).margin({ top: 20 })
- .onClick(() => {
- console.info('onTouch is clicked')
- this.text = "Button 'onTouch' is clicked"
- setTimeout(() => {
- let rect = Utils.getComponentRect('onMouse') // 獲取id為"onMouse"組件的矩形區(qū)域坐標
- let mouseEvent: MouseEvent = {
- button: MouseButton.Left,
- action: MouseAction.Press,
- x: rect.left + (rect.right - rect.left) / 2, // 組件中心點x坐標
- y: rect.top + (rect.bottom - rect.top) / 2, // 組件中心點y坐標
- screenX: rect.left + (rect.right - rect.left) / 2, // 組件中心點x坐標
- screenY: rect.top + (rect.bottom - rect.top) / 2, // 組件中心點y坐標
- timestamp: 1,
- target: {
- area: {
- width: 1,
- height: 1,
- position: {
- x: 1,
- y: 1
- },
- globalPosition: {
- x: 1,
- y: 1
- }
- }
- },
- source: SourceType.Mouse,
- pressure: 1,
- tiltX: 1,
- tiltY: 1,
- sourceTool: SourceTool.Unknown
- }
- sendMouseEvent(mouseEvent) // 發(fā)送鼠標事件
- }, 2000)
- }).id('onTouch')
- Button() {
- Text('onMouse').fontSize(25).fontWeight(FontWeight.Bold)
- }.margin({ top: 20 }).backgroundColor('#0D9FFB')
- .onMouse(() => {
- console.info('onMouse')
- this.text = "Button 'onMouse' in onMouse"
- setTimeout(() => {
- let keyEvent: KeyEvent = {
- type: KeyType.Down,
- keyCode: 2049,
- keyText: 'tab',
- keySource: 4,
- deviceId: 0,
- metaKey: 0,
- timestamp: 0
- }
- sendKeyEvent(keyEvent) // 發(fā)送按鍵事件
- }, 2000)
- }).id('onMouse')
- Text(this.text).fontSize(25).padding(15)
- }
- .width('100%').height('100%')
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: