如何实现点击按钮展示气泡,点击按钮不关闭气泡,仍保持展示状态,但是点击非按钮区域关闭气泡?
Popup气泡弹出时,默认有遮罩层(即参数mask默认为true),且页面有操作时气泡自动关闭(即参数autoCancel默认为true)。因此在点击按钮弹出气泡后,遮罩层覆盖整个页面,再次点击按钮,触发气泡自动关闭。
要实现点击按钮气泡不关闭保持展示状态,点击非按钮区域关闭气泡,可以禁用气泡遮罩层,通过为非按钮区域绑定onTouch事件自行处理气泡关闭逻辑。
- @Entry
- @Component
- struct PopupForClickButtonNotClose {
- @State handlePopup: boolean = false;
-
- @Builder
- popupBuilder() {
- Row({ space: 2 }) {
- Text('这里是自定义气泡的内容')
- .textAlign(TextAlign.Center)
- .fontSize(10);
- }.height(40).padding({ left: 10, right: 10 });
- }
-
- build() {
- Column({ space: 100 }) {
- Button('PopupOptions').margin({ top: 100 })
- .onClick(() => {
- this.handlePopup = true;
- })
- .onTouch((e) => {
- e.stopPropagation();
- }) // 阻止事件传递,避免气泡有关闭再打开的效果
- .bindPopup(this.handlePopup, {
- builder: this.popupBuilder,
- placement: Placement.Bottom,
- enableArrow: false, // 气泡弹出时不显示箭头
- targetSpace: '15vp',
- mask: false,
- autoCancel: false,
- onStateChange: (e) => {
- if (!e.isVisible) {
- this.handlePopup = false;
- }
- }
- });
- }
- .width('100%')
- .height('100%')
- .onTouch(() => {
- this.handlePopup = false;
- });
- }
- }