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

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

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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-02-01 00:36:43
相关问题
如何实现事件传,你会吗?
237浏览 • 1回复 待解决
如何将张图片转化为PixelMapElement
7866浏览 • 1回复 待解决
ets中如何将图片转为byte[]?
1286浏览 • 1回复 待解决
如何将 Checkbox 的文字放在左边 ?
8804浏览 • 1回复 待解决
如何将背景颜色设置为透明
294浏览 • 1回复 待解决
服务卡片java如何设置点击事件
7399浏览 • 1回复 待解决
如何将Resource资源对象转成string类型
405浏览 • 1回复 待解决
如何将页面设置为深色模式
274浏览 • 1回复 待解决
如何将像素点保存到图片文件
432浏览 • 1回复 待解决
如何将视频保存到相册以及主机端
3602浏览 • 1回复 待解决
鸿蒙如何将音频文件转成文本
2761浏览 • 1回复 待解决
如何将容器定位到屏幕的最底部
226浏览 • 1回复 待解决
如何将easymock的数据插入到数据库?
505浏览 • 1回复 待解决
如何将app.media.app_icon,转换为PixelMap
170浏览 • 1回复 待解决
如何将Ability的UI界面设置成透明
227浏览 • 1回复 待解决
如何将时间戳转换为日期格式的时间
362浏览 • 1回复 待解决