参考下面的Demo@Entry @Component struct BindPopupDemo { // 第一步:定义变量控制弹窗显示 @State customPopup: boolean = false; // 第二步:popup构造器定义弹框内容 @Builder customBubbleInstructionBuilder() { Stack() { Text() { Span("2021年12月20日-2022月1日30日每人限购8件 您已购买3件,还可购5件").fontSize(12) Span("您已购买3件,还可购5件").fontSize(14) }.fontColor(Color.White) .maxLines(2).textOverflow({ overflow: TextOverflow.Ellipsis }).ellipsisMode(EllipsisMode.END) }.backgroundColor(Color.Green) .width(100) .padding({ left: 12, right: 12, top: 18, bottom: 8 }) } build() { Column() { Button('click') // 第四步:创建点击事件,控制弹窗显隐 .onClick(() => { this.customPopup = !this.customPopup; }) .backgroundColor(0xf56c6c) // 第三步:使用bindPopup接口将弹窗内容绑定给元素 .bindPopup(this.customPopup, { builder: this.customBubbleInstructionBuilder, placement: Placement.Top, maskColor: 0x33000000, popupColor: Color.Green, enableArrow: true, radius: '10vp', // 设置气泡的圆角 backgroundBlurStyle: BlurStyle.NONE, // 设置箭头颜色 onStateChange: (e) => { if (!e.isVisible) { this.customPopup = false; } } }) } .justifyContent(FlexAlign.Center) .width('100%') .height(437) } }
参考下面的Demo