南 筏|
DevEco Studio
HarmonyOS 5
"兄弟"组件-如何做到事件透传?
已解决
代码如下,想实现,点击两个按钮的公共区域,同时输出对应的两个日志,如何实现?
.hitTestBehavior(HitTestMode.Transparent)无效
收起
自动换行
深色代码主题
复制
@Entry
@Component
struct HitTestBehaviorExample {
build() {
Stack() {
Stack() {
Button('inner button')
.width(50)
.height(200)
.hitTestBehavior(HitTestMode.Transparent)
.onClick(() => {
console.debug('ztq',`兄弟`)
})
Button('inner button')
.width(200)
.height(50)
.backgroundColor('#ff5500')
.hitTestBehavior(HitTestMode.Transparent)
.onClick(() => {
console.debug('ztq',`我在`)
})
}
.width("100%")
.height("100%")
}.width(300).height(300).backgroundColor('#F5F5F5')
}
} 5
浏览215 发布于2025-09-02 05:23江苏
全部评论
最多点赞
最新发布
最早发布
王德坤采纳答复
手势竞争之后只会有一个识别器胜出,而parallelGesture(并行手势绑定方法)只能在父子组件中起作用。
下边是我的蠢办法
收起
自动换行
深色代码主题
复制
@Entry
@Component
export struct ComponentTestPage {
checkIsInRect(rectOrg: Offset, rectSize: Size, pointOffset: Offset): boolean {
if (rectOrg.x < pointOffset.x && pointOffset.x < rectOrg.x + rectSize.width
&& rectOrg.y < pointOffset.y && pointOffset.y < rectOrg.y + rectSize.height) {
return true
}
return false
}
build() {
Stack() {
Text('回应 button')
.width(200)
.height(200)
.backgroundColor('#857')
.onTouch((e) => {
if (e.type === TouchType.Up) {
let comp1Info = this.getUIContext().getComponentUtils().getRectangleById('hy')
let comp1Offset = comp1Info.windowOffset
let comp1Size = comp1Info.size
// hilog.info(0, '[wdk]', `comp1Offset: ${JSON.stringify(comp1Offset)}`)
let comp2Info = this.getUIContext().getComponentUtils().getRectangleById('hj')
let comp2Offset = comp2Info.windowOffset
let comp2Size = comp2Info.size
// hilog.info(0, '[wdk]', `comp2Offset: ${JSON.stringify(comp2Offset)}`)
let hitOffsetX = this.getUIContext().vp2px(e.touches[0]?.displayX ?? 0)
let hitOffsetY = this.getUIContext().vp2px(e.touches[0]?.displayY ?? 0)
let hitOffset: Offset = {
x: hitOffsetX,
y: hitOffsetY
}
if (this.checkIsInRect(comp1Offset, comp1Size, hitOffset)
&& this.checkIsInRect(comp2Offset, comp2Size, hitOffset)) {
hilog.info(0, '[wdk]', `11: ${JSON.stringify(11)}`)
}
}
})
.id('hy')
Text('呼叫 button')
.width(200)
.height(200)
.backgroundColor('#ff5500')
.hitTestBehavior(HitTestMode.Transparent)
.onTouch((e) => {
if (e.type === TouchType.Up) {
let comp1Info = this.getUIContext().getComponentUtils().getRectangleById('hy')
let comp1Offset = comp1Info.windowOffset
let comp1Size = comp1Info.size
// hilog.info(0, '[wdk]', `comp1Offset: ${JSON.stringify(comp1Offset)}`)
let comp2Info = this.getUIContext().getComponentUtils().getRectangleById('hj')
let comp2Offset = comp2Info.windowOffset
let comp2Size = comp2Info.size
// hilog.info(0, '[wdk]', `comp2Offset: ${JSON.stringify(comp2Offset)}`)
let hitOffsetX = this.getUIContext().vp2px(e.touches[0]?.displayX ?? 0)
let hitOffsetY = this.getUIContext().vp2px(e.touches[0]?.displayY ?? 0)
let hitOffset: Offset = {
x: hitOffsetX,
y: hitOffsetY
}
if (this.checkIsInRect(comp1Offset, comp1Size, hitOffset)
&& this.checkIsInRect(comp2Offset, comp2Size, hitOffset)) {
hilog.info(0, '[wdk]', `22: ${JSON.stringify(22)}`)
}
}
})
.id('hj')
.margin({
left: 150,
top: 100
})
}
.width(300)
.height(300)
.backgroundColor('#F5F5F5')
}
} 展开全部 
2楼回复于2025-09-03 07:41 来自北京
南 筏这办法太神了,谢谢老哥!等我研究一下!这确实可行!2025-09-04 02:10 来自江苏


感觉不是特别合理,哎,暂时也没有更好的办法了
收起
自动换行
深色代码主题
复制
import componentUtils from "@ohos.arkui.componentUtils"
@Entry
@Component
export struct ComponentTestPage {
checkIsInRect(rectOrg: componentUtils.Offset, rectSize: componentUtils.Size, pointOffset: componentUtils.Offset): boolean {
if (rectOrg.x < pointOffset.x && pointOffset.x < rectOrg.x + rectSize.width
&& rectOrg.y < pointOffset.y && pointOffset.y < rectOrg.y + rectSize.height) {
return true
}
return false
}
build() {
Stack() {
Text('回应 button')
.width(200)
.height(200)
.backgroundColor('#857')
.onTouch((e) => {
if (e.type === TouchType.Up) {
let comp1Info = this.getUIContext().getComponentUtils().getRectangleById('hy')
let comp1Offset = comp1Info.windowOffset
let comp1Size = comp1Info.size
// hilog.info(0, '[wdk]', `comp1Offset: ${JSON.stringify(comp1Offset)}`)
let comp2Info = this.getUIContext().getComponentUtils().getRectangleById('hj')
let comp2Offset = comp2Info.windowOffset
let comp2Size = comp2Info.size
// hilog.info(0, '[wdk]', `comp2Offset: ${JSON.stringify(comp2Offset)}`)
let hitOffsetX = this.getUIContext().vp2px(e.touches[0]?.displayX ?? 0)
let hitOffsetY = this.getUIContext().vp2px(e.touches[0]?.displayY ?? 0)
let hitOffset: componentUtils.Offset = {
x: hitOffsetX,
y: hitOffsetY
}
if (this.checkIsInRect(comp1Offset, comp1Size, hitOffset)
&& this.checkIsInRect(comp2Offset, comp2Size, hitOffset)) {
console.debug('ztq', '兄弟')
}
}
})
.id('hy')
Text('呼叫 button')
.width(200)
.height(200)
.backgroundColor('#ff5500')
.hitTestBehavior(HitTestMode.Transparent)
.onTouch((e) => {
if (e.type === TouchType.Up) {
let comp1Info = this.getUIContext().getComponentUtils().getRectangleById('hy')
let comp1Offset = comp1Info.windowOffset
let comp1Size = comp1Info.size
// hilog.info(0, '[wdk]', `comp1Offset: ${JSON.stringify(comp1Offset)}`)
let comp2Info = this.getUIContext().getComponentUtils().getRectangleById('hj')
let comp2Offset = comp2Info.windowOffset
let comp2Size = comp2Info.size
// hilog.info(0, '[wdk]', `comp2Offset: ${JSON.stringify(comp2Offset)}`)
let hitOffsetX = this.getUIContext().vp2px(e.touches[0]?.displayX ?? 0)
let hitOffsetY = this.getUIContext().vp2px(e.touches[0]?.displayY ?? 0)
let hitOffset: componentUtils.Offset = {
x: hitOffsetX,
y: hitOffsetY
}
if (this.checkIsInRect(comp1Offset, comp1Size, hitOffset)
&& this.checkIsInRect(comp2Offset, comp2Size, hitOffset)) {
console.debug('ztq', '我在')
}
}
})
.id('hj')
.margin({
left: 150,
top: 100
})
}
.width(300)
.height(300)
.backgroundColor('#F5F5F5')
}
} 展开全部 
3楼回复于2026-01-15 02:00 来自江苏
王德坤1楼回复于2025-09-02 05:54 来自北京
南 筏讲道理,为啥不好使呢!!!是按照文档来的呀~ @Entry @Component struct HitTestBehaviorExample { build() { Stack() { Button('回应 button') .width(200) .height(200) .backgroundColor('#857') .onClick(() => { console.debug('ztq',`我在`) }) Column(){ Button('呼叫 button') .width(200) .height(200) .backgroundColor('#ff5500') .onClick(() => { console.debug('ztq',`兄弟`) }) }.width(200) .height(200) .hitTestBehavior(HitTestMode.Transparent) }.width(300).height(300).backgroundColor('#F5F5F5') } }2025-09-02 06:39 来自江苏
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
相关推荐
换一批
1
HarmonyOS 目前是否有组件支持markdown数据展示(API12+)
2
HarmonyOS 如何实现一个自动换行的组件(API12+)
3
HarmonyOS 自定义C-API组件如何显示图片 (API12+)
4
HarmonyOS 如何实现显示倒计时的组件,比如倒计时24小时?(API12+)
5
HarmonyOS bindPopup弹框如何实现事件透传?(API12+)
6
HarmonyOS 如何实现点击Menu外部事件可透传(API12+)
7
HarmonyOS 组件A通过bindContextMenu配置了长按菜单,点击菜单外区域,组件A响应了点击事件(API20+)
8
HarmonyOS 嵌套的hitTestBehavior,子组件不生效(API12+)
了解社区公约,与您携手共创和谐专业的开发者社区。




































苏公安网备 32011402010933号