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

HarmonyOS
2024-12-04 13:56:25
浏览
收藏 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 + ')');
          }
        });
    }
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-04 16:57:51


相关问题
HarmonyOS onClick事件如何阻止事件冒泡
778浏览 • 1回复 待解决