大网数据广告活动
OpenHarmony 设备与应用开发

OpenHarmony 中文参考:PopupV2

· 52CE 我爱测

该组件基于状态管理(V2)实现,相较于状态管理(V1),状态管理(V2)增强了对数据对象的深度观察与管理能力,不再局限于组件层级。借助状态管理(V2),开发者可以通过该组件更灵活地控制显示特定样式的气泡,实现更高效的用户界面刷新。

PopupV2

PopupV2用于显示特定样式的气泡。

该组件基于状态管理(V2)实现,相较于状态管理(V1),状态管理(V2)增强了对数据对象的深度观察与管理能力,不再局限于组件层级。借助状态管理(V2),开发者可以通过该组件更灵活地控制显示特定样式的气泡,实现更高效的用户界面刷新。

起始版本: 26.0.0

导入模块

子组件

无

PopupV2

PopupV2(options: PopupV2InitInfo): void

起始版本: 26.0.0

装饰器类型: @Builder

原子化服务API: 从API版本26.0.0开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

设备行为差异: 该接口在Wearable设备上使用时,应用程序运行异常,异常信息中提示接口未定义,在其他设备中可正常调用。

参数:

参数名 类型 必填 说明
options PopupV2InitInfo 是 定义PopupV2组件的配置参数。

PopupV2InitInfo

定义PopupV2的具体样式参数。

起始版本: 26.0.0

原子化服务API: 从API版本26.0.0开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

设备行为差异: 该接口在Wearable设备上使用时,应用程序运行异常,异常信息中提示接口未定义,在其他设备中可正常调用。

名称 类型 只读 可选 说明
icon ResourceStr 否 是 设置PopupV2图标。
说明: 默认值:'',不显示图标。
title ResourceStr 否 是 设置PopupV2标题文本。
说明: 默认值:'',不显示标题文本。
message ResourceStr 是 否 设置PopupV2内容文本。
说明: 默认值:'',不显示内容文本。
titleModifier TextModifier 否 是 设置标题文本属性,如设置标题颜色、字体大小、字重等。
默认值:undefined,使用系统标题文本属性。
iconModifier ImageModifier 否 是 设置图标属性,如图标颜色、大小、边框等。
默认值:undefined,使用系统图标属性。
messageModifier TextModifier 否 是 设置内容文本属性,如设置内容文本颜色、字体大小、字重等。
默认值:undefined,使用系统内容文本属性。
showClose boolean | Resource 否 是 设置PopupV2关闭按钮。true:显示关闭按钮;false:不显示关闭按钮。Resource类型:显示对应的图标。
默认值:true
onClose () => void 否 是 设置PopupV2关闭按钮回调函数。
默认不设置关闭按钮回调函数。
buttons [PopupV2Button?,PopupV2Button?] 否 是 设置PopupV2操作按钮,按钮最多设置两个。默认不显示按钮。
默认值:[{ text: '' }, { text: '' }]
direction Direction 否 是 布局方向。
默认值:Direction.Auto
maxWidth Dimension 否 是 设置PopupV2的最大宽度,通过此接口PopupV2可以自定义宽度显示。
默认值:400vp
说明:
1. 在使用引用资源类型时,规定其参数类型要与属性方法本身类型一致。
2. maxWidth是数字类型,支持float和整型,例如$r('app.float.maxWidth')、$r('app.integer.maxWidth')。
3. 当类型为Resource时,如果未设置单位,默认单位为px。

PopupV2Button

PopupV2Button定义按钮的相关属性和事件。

起始版本: 26.0.0

原子化服务API: 从API版本26.0.0开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

设备行为差异: 该接口在Wearable设备上使用时,应用程序运行异常,异常信息中提示接口未定义,在其他设备中可正常调用。

名称 类型 只读 可选 说明
text ResourceStr 否 否 设置按钮内容。
action () => void 否 是 设置按钮点击回调。
默认不执行任何操作。
buttonTextModifier TextModifier 否 是 设置按钮文本属性,如设置文本颜色、字体大小等。默认值:undefined,值为undefined时,默认使用系统按钮文本属性。

示例

示例1(设置气泡样式)

该示例通过配置titleModifier、messageModifier、PopupV2Button实现气泡样式。

从API版本26.0.0开始,新增titleModifier、messageModifier、PopupV2Button。

// xxx.ets

@Entry
@ComponentV2
struct PopupExample {

  build() {
    Row() {
      // PopupV2自定义高级组件
      PopupV2 ({
        // 请开发者替换为实际的资源文件
        icon:  $r('app.media.startIcon'),
        iconModifier: new ImageModifier().width(32).height(32).fillColor(Color.White).borderRadius(16),
        title: 'This is a popupv2',
        titleModifier: new TextModifier().fontSize(20).fontColor(Color.Black).fontWeight(FontWeight.Normal),
        message:  'This is the message',
        messageModifier: new TextModifier().fontSize(15).fontColor(Color.Black),
        showClose: false,
        onClose: () => {
          console.info('close Button click');
        },
        buttons: [{
          text: 'confirm',
          action: () => {
            console.info('confirm button click');
          },
          buttonTextModifier: new TextModifier().fontSize(15).fontColor(Color.Black)
        },
          {
            text: 'cancel',
            action: () => {
              console.info('cancel button click');
            },
            buttonTextModifier: new TextModifier().fontSize(15).fontColor(Color.Black)
          }] as [PopupV2Button| undefined, PopupV2Button|undefined]
      })
    }
    .width(300)
    .height(200)
    .borderWidth(2)
    .justifyContent(FlexAlign.Center)
  }
}

示例2(设置镜像效果)

该示例通过配置direction实现镜像布局效果。

从API版本26.0.0开始,新增direction参数。

// xxx.ets

@Entry
@ComponentV2
struct PopupExample {

  build() {
    Column() {
      // PopupV2自定义高级组件
      PopupV2 ({
        direction: Direction.Rtl,
        // 请开发者替换为实际的资源文件
        icon:  $r('app.media.startIcon'),
        iconModifier: new ImageModifier().width(32).height(32).fillColor(Color.White).borderRadius(16),
        title: 'This is a popupv2',
        titleModifier: new TextModifier().fontSize(20).fontColor(Color.Black).fontWeight(FontWeight.Normal),
        message:  'This is the message',
        messageModifier: new TextModifier().fontSize(15).fontColor(Color.Black),
        showClose: true,
        onClose: () => {
          console.info('close Button click');
        },
        buttons: [{
          text: 'confirm',
          action: () => {
            console.info('confirm button click');
          },
          buttonTextModifier: new TextModifier().fontSize(15).fontColor(Color.Black)
        },
          {
            text: 'cancel',
            action: () => {
              console.info('cancel button click');
            },
            buttonTextModifier: new TextModifier().fontSize(15).fontColor(Color.Black)
          }] as [PopupV2Button| undefined, PopupV2Button|undefined]
      })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

示例3(设置自定义宽度)

该示例通过配置maxWidth实现自定义宽度效果。

从API版本26.0.0开始,新增maxWidth参数。

// xxx.ets

@Entry
@ComponentV2
struct PopupExample {

  build() {
    Row() {
      // PopupV2自定义高级组件
      PopupV2 ({
        maxWidth: '50%',
        // 请开发者替换为实际的资源文件
        icon:  $r('app.media.startIcon'),
        iconModifier: new ImageModifier().width(32).height(32).fillColor(Color.White).borderRadius(16),
        title: 'This is a popupv2',
        titleModifier: new TextModifier().fontSize(20).fontColor(Color.Black).fontWeight(FontWeight.Normal),
        message:  'This is the message, This is the message, This is the message, This is the message',
        messageModifier: new TextModifier().fontSize(15).fontColor(Color.Black),
        showClose: true,
        onClose: () => {
          console.info('close Button click');
        },
        buttons: [{
          text: 'confirm',
          action: () => {
            console.info('confirm button click');
          },
          buttonTextModifier: new TextModifier().fontSize(15).fontColor(Color.Black)
        },
          {
            text: 'cancel',
            action: () => {
              console.info('cancel button click');
            },
            buttonTextModifier: new TextModifier().fontSize(15).fontColor(Color.Black)
          }] as [PopupV2Button| undefined, PopupV2Button|undefined]
      })
    }
    .width(400)
    .height(200)
    .borderWidth(2)
    .justifyContent(FlexAlign.Center)
  }
}

本文来自 OpenHarmony 文档贡献者,按 CC BY 4.0 整理版式与相对链接,保留正文与代码。图示请参阅原文。作者与贡献记录。

资料来源与适用范围

资料来源
OpenHarmony 文档贡献者
适用范围
master 文档快照(2026-11-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。