參考文檔:https://gitee.com/openharmony/docs/blob/5654c2b940ab3e2f4f0baf435e630c4ef3536428/zh-cn/application-dev/reference/arkui-ts/ts-universal-attributes-component-id.md
定向發送事件
元件辨別在文檔中一個最重要的能力,就是可以向指定元件發送事件(現在隻支援單擊click與長按longpress)
sendEventByKey方法可以使用,但在IDE會顯示報錯,建議@ts-ignore忽略錯誤
@State result: string = ''
build() {
Flex({
alignItems: ItemAlign.Center,
justifyContent: FlexAlign.Center,
direction: FlexDirection.Column,
}) {
Text('元件辨別發送事件示範')
.fontSize(30)
.margin({ bottom: 30 })
.id('testComponent')
.gesture(LongPressGesture().onActionEnd(() => {
this.result = 'TEXT元件被長按了'
}))
.onClick(() => {
this.result = 'TEXT元件被點選了'
})
Text(this.result).fontSize(20).margin({ bottom: 30 }).fontColor(Color.Green)
Button('發送點選事件').onClick(() => {
// @ts-ignore
sendEventByKey("testComponent", 10, "test")
})
.margin({ bottom: 20 })
Button('發送長按事件').onClick(() => {
// @ts-ignore
sendEventByKey("testComponent", 11, "test")
})
}
.width('100%')
.height('100%')
}