id組件標識

2024-01-22 16:22 更新

id為組件的唯一標識,在整個應用內唯一。本模塊提供組件標識相關接口,可以獲取指定id組件的屬性,也提供向指定id組件發(fā)送事件的功能。

說明

從API Version 8開始支持。后續(xù)版本如有新增內容,則采用上角標單獨標記該內容的起始版本。

屬性

名稱參數說明描述
idstring

組件的唯一標識,唯一性由使用者保證。

默認值:''

從API version 9開始,該接口支持在ArkTS卡片中使用。

接口

getInspectorByKey9+

getInspectorByKey(id: string): string

獲取指定id的組件的所有屬性,不包括子組件信息。

此接口僅用于對應用的測試。

參數:

參數類型必填描述
idstring要獲取屬性的組件id。

返回值:

類型描述
string組件屬性列表的JSON字符串。

getInspectorTree9+

getInspectorTree(): Object

獲取組件樹及組件屬性。

此接口僅用于對應用的測試。

返回值:

類型描述
Object組件樹及組件屬性列表的JSON對象。

sendEventByKey9+

sendEventByKey(id: string, action: number, params: string): boolean

給指定id的組件發(fā)送事件。

此接口僅用于對應用的測試。

參數:

參數類型必填描述
idstring要觸發(fā)事件的組件的id。
actionnumber

要觸發(fā)的事件類型,目前支持取值:

- 點擊事件Click: 10

- 長按事件LongClick: 11。

paramsstring事件參數,無參數傳空字符串 ""。

返回值:

類型描述
boolean找不到指定id的組件時返回false,其余情況返回true。

sendTouchEvent9+

sendTouchEvent(event: TouchObject): boolean

發(fā)送觸摸事件。

此接口僅用于對應用的測試。

參數:

參數類型必填描述
eventTouchObject觸發(fā)觸摸事件的位置,event參數見TouchEvent中TouchObject的介紹。

返回值:

類型描述
boolean事件發(fā)送失敗時返回false,其余情況返回true。

sendKeyEvent9+

sendKeyEvent(event: KeyEvent): boolean

發(fā)送按鍵事件。

此接口僅用于對應用的測試。

參數:

參數類型必填描述
eventKeyEvent按鍵事件,event參數見KeyEvent介紹。

返回值:

類型描述
boolean事件發(fā)送失敗時時返回false,其余情況返回true。

sendMouseEvent9+

sendMouseEvent(event: MouseEvent): boolean

發(fā)送鼠標事件。

此接口僅用于對應用的測試。

參數:

參數類型必填描述
eventMouseEvent鼠標事件,event參數見MouseEvent介紹。

返回值:

類型描述
boolean事件發(fā)送失敗時返回false,其余情況返回true。

示例

  1. // xxx.ets
  2. class Utils {
  3. static rect_left
  4. static rect_top
  5. static rect_right
  6. static rect_bottom
  7. static rect_value
  8. //獲取組件所占矩形區(qū)域坐標
  9. static getComponentRect(key) {
  10. let strJson = getInspectorByKey(key)
  11. let obj = JSON.parse(strJson)
  12. console.info("[getInspectorByKey] current component obj is: " + JSON.stringify(obj))
  13. let rectInfo = JSON.parse('[' + obj.$rect + ']')
  14. console.info("[getInspectorByKey] rectInfo is: " + rectInfo)
  15. this.rect_left = JSON.parse('[' + rectInfo[0] + ']')[0]
  16. this.rect_top = JSON.parse('[' + rectInfo[0] + ']')[1]
  17. this.rect_right = JSON.parse('[' + rectInfo[1] + ']')[0]
  18. this.rect_bottom = JSON.parse('[' + rectInfo[1] + ']')[1]
  19. return this.rect_value = {
  20. "left": this.rect_left, "top": this.rect_top, "right": this.rect_right, "bottom": this.rect_bottom
  21. }
  22. }
  23. }
  24. @Entry
  25. @Component
  26. struct IdExample {
  27. @State text: string = ''
  28. build() {
  29. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  30. Button() {
  31. Text('onKeyTab').fontSize(25).fontWeight(FontWeight.Bold)
  32. }.margin({ top: 20 }).backgroundColor('#0D9FFB')
  33. .onKeyEvent(() => {
  34. this.text = "onKeyTab"
  35. })
  36. Button() {
  37. Text('click to start').fontSize(25).fontWeight(FontWeight.Bold)
  38. }.margin({ top: 20 })
  39. .onClick(() => {
  40. console.info(getInspectorByKey("click"))
  41. console.info(JSON.stringify(getInspectorTree()))
  42. this.text = "Button 'click to start' is clicked"
  43. setTimeout(() => {
  44. sendEventByKey("longClick", 11, "") // 向id為"longClick"的組件發(fā)送長按事件
  45. }, 2000)
  46. }).id('click')
  47. Button() {
  48. Text('longClick').fontSize(25).fontWeight(FontWeight.Bold)
  49. }.margin({ top: 20 }).backgroundColor('#0D9FFB')
  50. .gesture(
  51. LongPressGesture().onActionEnd(() => {
  52. console.info('long clicked')
  53. this.text = "Button 'longClick' is longclicked"
  54. setTimeout(() => {
  55. let rect = Utils.getComponentRect('onTouch') // 獲取id為"onTouch"組件的矩形區(qū)域坐標
  56. let touchPoint: TouchObject = {
  57. id: 1,
  58. x: rect.left + (rect.right - rect.left) / 2, // 組件中心點x坐標
  59. y: rect.top + (rect.bottom - rect.top) / 2, // 組件中心點y坐標
  60. type: TouchType.Down,
  61. screenX: rect.left + (rect.right - rect.left) / 2, // 組件中心點x坐標
  62. screenY: rect.left + (rect.right - rect.left) / 2, // 組件中心點y坐標
  63. }
  64. sendTouchEvent(touchPoint) // 發(fā)送觸摸事件
  65. touchPoint.type = TouchType.Up
  66. sendTouchEvent(touchPoint) // 發(fā)送觸摸事件
  67. }, 2000)
  68. })).id('longClick')
  69. Button() {
  70. Text('onTouch').fontSize(25).fontWeight(FontWeight.Bold)
  71. }.type(ButtonType.Capsule).margin({ top: 20 })
  72. .onClick(() => {
  73. console.info('onTouch is clicked')
  74. this.text = "Button 'onTouch' is clicked"
  75. setTimeout(() => {
  76. let rect = Utils.getComponentRect('onMouse') // 獲取id為"onMouse"組件的矩形區(qū)域坐標
  77. let mouseEvent: MouseEvent = {
  78. button: MouseButton.Left,
  79. action: MouseAction.Press,
  80. x: rect.left + (rect.right - rect.left) / 2, // 組件中心點x坐標
  81. y: rect.top + (rect.bottom - rect.top) / 2, // 組件中心點y坐標
  82. screenX: rect.left + (rect.right - rect.left) / 2, // 組件中心點x坐標
  83. screenY: rect.top + (rect.bottom - rect.top) / 2, // 組件中心點y坐標
  84. timestamp: 1,
  85. target: {
  86. area: {
  87. width: 1,
  88. height: 1,
  89. position: {
  90. x: 1,
  91. y: 1
  92. },
  93. globalPosition: {
  94. x: 1,
  95. y: 1
  96. }
  97. }
  98. },
  99. source: SourceType.Mouse,
  100. pressure: 1,
  101. tiltX: 1,
  102. tiltY: 1,
  103. sourceTool: SourceTool.Unknown
  104. }
  105. sendMouseEvent(mouseEvent) // 發(fā)送鼠標事件
  106. }, 2000)
  107. }).id('onTouch')
  108. Button() {
  109. Text('onMouse').fontSize(25).fontWeight(FontWeight.Bold)
  110. }.margin({ top: 20 }).backgroundColor('#0D9FFB')
  111. .onMouse(() => {
  112. console.info('onMouse')
  113. this.text = "Button 'onMouse' in onMouse"
  114. setTimeout(() => {
  115. let keyEvent: KeyEvent = {
  116. type: KeyType.Down,
  117. keyCode: 2049,
  118. keyText: 'tab',
  119. keySource: 4,
  120. deviceId: 0,
  121. metaKey: 0,
  122. timestamp: 0
  123. }
  124. sendKeyEvent(keyEvent) // 發(fā)送按鍵事件
  125. }, 2000)
  126. }).id('onMouse')
  127. Text(this.text).fontSize(25).padding(15)
  128. }
  129. .width('100%').height('100%')
  130. }
  131. }
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號