3.8、HarmonyOS Next 气泡提示(Popup)

Popup 属性可绑定在组件上显示气泡弹窗提示,设置弹窗内容、交互逻辑和显示状态。主要用于屏幕录制、信息弹出提醒等显示状态。

气泡分为两种类型,一种是系统提供的气泡 PopupOptions,一种是开发者可以自定义的气泡 CustomPopupOptions。其中 PopupOptions 为系统提供的气泡,通过配置 primaryButtonsecondaryButton 来设置带按钮的气泡。CustomPopupOptions 通过配置 builder 参数来设置自定义的气泡。

文本提示气泡

文本提示气泡 常用于只展示带有文本的信息提示,不带有任何交互的场景。Popup 属性需绑定组件,当 bindPopup 属性中参数 showtrue 的时候会弹出气泡提示。

Button 组件上绑定 Popup 属性,每次点击 Button 按钮,handlePopup 会切换布尔值,当其为 true 时,触发 bindPopup 弹出气泡。

ts 复制代码
@Entry
@Component
struct PopupPage {

  @State handlePopup: boolean = false
  
  build() {
    Navigation() {
      Button('PopupOptions')
        .onClick(() => {
          this.handlePopup = !this.handlePopup
        })
        .bindPopup(this.handlePopup, {
          message: 'This is a popup with PopupOptions',
        }).margin({left:15,bottom:15, top:15})
    }
    .title('创建文本')
    .titleMode(NavigationTitleMode.Mini)
  }
}

添加气泡状态变化的事件

通过 onStateChange 参数为气泡添加状态变化的事件回调,可以判断当前气泡的显示状态。

ts 复制代码
@Entry
@Component
struct PopupPage {

  @State handlePopup: boolean = false
  
  build() {
    Navigation() {
      Button('PopupOptions')
        .onClick(() => {
          this.handlePopup = !this.handlePopup
        })
        .bindPopup(this.handlePopup, {
          message: 'This is a popup with PopupOptions',
          onStateChange: (e)=> { // 返回当前的气泡状态
           if (!e.isVisible) {
             this.handlePopup = false
           }
         }
        }).margin({left:15,bottom:15, top:15})
    }
    .title('创建文本')
    .titleMode(NavigationTitleMode.Mini)
  }
}

带按钮的提示气泡

通过 primaryButtonsecondaryButton 属性为气泡最多设置两个 Button 按钮,通过此按钮进行简单的交互,开发者可以通过配置 action 参数来设置想要触发的操作。

ts 复制代码
Button('PopupOptions')
      .onClick(() => {
        this.handlePopup = !this.handlePopup
      })
      .bindPopup(this.handlePopup, {
        message: 'This is a popup with PopupOptions',
        primaryButton: {
          value: 'Confirm',
          action: () => {
            this.handlePopup = !this.handlePopup
            console.info('confirm Button click')
          }
        },
        secondaryButton: {
          value: 'Cancel',
          action: () => {
            this.handlePopup = !this.handlePopup
          }
        },
        onStateChange: (e) => {
          if (!e.isVisible) {
            this.handlePopup = false
          }
        }
      }).margin({left:15,bottom:15, top:15})

自定义气泡

开发者可以使用构建器 CustomPopupOptions 创建自定义气泡,@Builder 中可以放自定义的内容。除此之外,还可以通过 popupColor 等参数控制气泡样式。

ts 复制代码
 @Builder example4() {
    Button('PopupOptions')
      .onClick(() => {
        this.handlePopup = !this.handlePopup
      })
      .bindPopup(this.handlePopup, {
        builder: this.popupBuilder(), // 气泡的内容
        placement:Placement.Bottom, // 气泡的弹出位置
        popupColor:Color.Pink, // 气泡的背景色
        onStateChange: (e)=> { // 返回当前的气泡状态
          if (!e.isVisible) {
            this.handlePopup = false
          }
        }
      }).margin({left:15,bottom:15, top:15})
  }

  // popup构造器定义弹框内容
  @Builder popupBuilder() {
    Row({ space: 2 }) {
      Image($r("app.media.icon")).width(24).height(24).margin({ left: 5 })
      Text('This is Custom Popup').fontSize(15)
    }.width(200).height(50).padding(5)
  }

上一篇 3.7、自定义弹窗(CustomDialog) 下一篇 3.9、菜单(Menu)

相关推荐
进击的前栈22 分钟前
Flutter跨平台开发鸿蒙化HTTP解析工具包使用指南
flutter·http·harmonyos
进击的前栈2 小时前
Flutter跨平台开发鸿蒙化HTTP测试工具包使用指南
flutter·http·harmonyos
花开彼岸天~7 小时前
Flutter跨平台开发鸿蒙化定位服务组件使用指南
flutter·开源·harmonyos
特立独行的猫a7 小时前
移植开源软件Notepad--(NDD)到鸿蒙PC:环境搭建与配置
notepad++·开源软件·harmonyos·鸿蒙pc·notpad--
鸿蒙开发工程师—阿辉8 小时前
HarmonyOS 上下文的使用: 脱离 UI 怎么用 Context?
ui·华为·harmonyos
FrameNotWork9 小时前
HarmonyOS 教学实战(八):后台任务、Worker 与任务调度
华为·harmonyos
¥-oriented10 小时前
【鸿蒙相关活动】
harmonyos
梦想不只是梦与想12 小时前
鸿蒙中 倒计时实现方法
华为·harmonyos·鸿蒙·setinterval·texttimer
sinat_3842410921 小时前
HarmonyOS音乐播放器开发实战:从零到一打造完整鸿蒙系统音乐播放器应用 2
华为·gitlab·intellij-idea·harmonyos·visual studio·webstorm
waeng_luo1 天前
HarmonyOS开发-多线程与异步编程
harmonyos·鸿蒙2025领航者闯关·鸿蒙6实战·#鸿蒙2025领航者闯关