点击两个按钮的公共区域,同时输出两个按钮对应的日志内容,如何实现,问题代码如下:
@Entry
@Component
struct HitTestBehaviorExample {
build() {
Stack() {
Stack() {
Button('Button1')
.width(50)
.height(200)
.hitTestBehavior(HitTestMode.Transparent)
.onClick(() => {
console.debug('Button1',`点击了`)
})
Button('Button2')
.width(200)
.height(50)
.backgroundColor('#ff5500')
.hitTestBehavior(HitTestMode.Transparent)
.onClick(() => {
console.debug('Button2',`点击了`)
})
}
.width("100%")
.height("100%")
}.width(300).height(300).backgroundColor('#F5F5F5')
}
} 运行结果如下,Button1按钮没有触发点击事件回调:
09-01 22:18:30.675 6833-6833 A03d00/JSAPP com.lee.m...ication2 D Button2 点击了
使用onTouch事件透传,修改后的代码如下:
@Entry
@Component
struct HitTestBehaviorExample {
build() {
Stack() {
Stack() {
Button('button1')
.width(50)
.height(200)
.hitTestBehavior(HitTestMode.Transparent)
.onTouch((event: TouchEvent) => {
if (event.type == TouchType.Down) {
console.debug('button1', `点击了`);
}
});
Button('button2')
.width(200)
.height(50)
.backgroundColor('#ff5500')
.hitTestBehavior(HitTestMode.Transparent)
.onTouch((event: TouchEvent) => {
if (event.type == TouchType.Down) {
console.debug('button2', `点击了`);
}
});
}
.width('100%')
.height('100%');
}.width(300).height(300).backgroundColor('#F5F5F5');
}
} 运行结果如下:
