如何将点击事件透传到下一层

如何将点击事件透传到下一层

HarmonyOS
2024-01-30 20:32:29
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
smallmeteror

可以将当前组件的点击事件发送给目标组件,触发目标组件的点击事件,详细可以参考:sendEventByKey

示例代码如下:

class Utils { 
  static rect_left: number 
  static rect_top: number 
  static rect_right: number 
  static rect_bottom: number 
  static rect_value: Record<string, number> 
  //获取组件所占矩形区域坐标 
  static getComponentRect(key:string):Record<string, number> { 
    let strJson = getInspectorByKey(key) 
    let obj:Record<string, string> = JSON.parse(strJson) 
    console.info("[getInspectorByKey] current component obj is: " + JSON.stringify(obj)) 
    let rectInfo:string[] = JSON.parse('[' + obj.$rect + ']') 
    console.info("[getInspectorByKey] rectInfo is: " + rectInfo) 
    Utils.rect_left = JSON.parse('[' + rectInfo[0] + ']')[0] 
    Utils.rect_top = JSON.parse('[' + rectInfo[0] + ']')[1] 
    Utils.rect_right = JSON.parse('[' + rectInfo[1] + ']')[0] 
    Utils.rect_bottom = JSON.parse('[' + rectInfo[1] + ']')[1] 
    return Utils.rect_value = { 
      "left": Utils.rect_left, "top": Utils.rect_top, "right": Utils.rect_right, "bottom": Utils.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"的组件发送长按事件 
        }, 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"组件的矩形区域坐标 
            let touchPoint: TouchObject = { 
              id: 1, 
              type: TouchType.Down, 
              x: rect.left + (rect.right - rect.left) / 2, 
              y: rect.top + (rect.bottom - rect.top) / 2, 
              screenX: rect.left + (rect.right - rect.left) / 2, 
              screenY: rect.left + (rect.right - rect.left) / 2, 
              windowX: rect.left + (rect.right - rect.left) / 2, 
              windowY: rect.left + (rect.right - rect.left) / 2, 
              displayX: rect.left + (rect.right - rect.left) / 2, 
              displayY: rect.left + (rect.right - rect.left) / 2, 
            } 
            sendTouchEvent(touchPoint) // 发送触摸事件 
            touchPoint.type = TouchType.Up 
            sendTouchEvent(touchPoint) // 发送触摸事件 
          }, 2000) 
        })).id('longClick') 
      Text(this.text).fontSize(25).padding(15) 
    } 
    .width('100%').height('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
分享
微博
QQ
微信
回复
2024-02-01 00:36:43


相关问题
HarmonyOS 弹点击事件传处理
547浏览 • 1回复 待解决
怎么传onClick事件
598浏览 • 1回复 待解决
如何实现事件传,你会吗?
2846浏览 • 1回复 待解决
如何将张图片转化为PixelMapElement
10678浏览 • 1回复 待解决
HarmonyOS 如何将个方法标记为过时
402浏览 • 1回复 待解决