#鸿蒙通关秘籍#如何阻止鸿蒙系统中onMouse事件的冒泡?

HarmonyOS
20h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
IoT风中琴

onMouse事件回调中调用stopPropagation()方法可以阻止事件冒泡,如下所示:

class ish {
  isHovered: boolean = false;
  set(val: boolean) {
    this.isHovered = val;
  }
}

class butf {
  buttonText: string = '';
  set(val: string) {
    this.buttonText = val;
  }
}

@Entry
@Component
struct MouseExample {
  @State isHovered: ish = new ish();

  build() {
    Column() {
      Button(this.isHovered ? 'Hovered!' : 'Not Hover')
        .width(200)
        .height(100)
        .backgroundColor(this.isHovered ? Color.Green : Color.Gray)
        .onHover((isHover?: boolean) => {
          if(isHover) {
            let ishset = new ish();
            ishset.set(isHover);
          }
        })
        .onMouse((event?: MouseEvent) => {
          if (event) {
            if (event.stopPropagation) {
              event.stopPropagation();
            }
            let butset = new butf();
            butset.set('Button onMouse:\n' + '' +
                      'button = ' + event.button + '\n' +
                      'action = ' + event.action + '\n' +
                      'x,y = (' + event.x + ',' + event.y + ')' + '\n' +
                      'windowXY=(' + event.windowX + ',' + event.windowY + ')');
          }
        });
    }
  }
}
分享
微博
QQ
微信
回复
17h前
相关问题
如何阻止Flex容器鼠标事件穿透
1962浏览 • 1回复 待解决