openharmony 鸿蒙 ohos-arkui-advanced-Popup

2025-06-12 浏览 (1)

Popup

The Popup component is used to display popups in a specific style.

NOTE

  • This component is supported since API version 11. Updates will be marked with a superscript to indicate their earliest API version.

  • Use this component with the custom popup features in popup control for best results.

Modules to Import

import { Popup, PopupOptions, PopupTextOptions, PopupButtonOptions, PopupIconOptions } from '@kit.ArkUI';

Child Components

Not supported

Popup

Popup(options: PopupOptions): void

Decorator: @Builder

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
optionsPopupOptionsYesParameters of the popup.

PopupOptions

Defines the style parameters of the popup.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
iconPopupIconOptionsNoIcon of the popup.
NOTE
The icon is not displayed when size is set to an invalid value or 0.
Atomic service API: This API can be used in atomic services since API version 12.
titlePopupTextOptionsNoTitle of the popup.
Atomic service API: This API can be used in atomic services since API version 12.
messagePopupTextOptionsYesContent of the popup.
NOTE
fontWeight is not available for messages.
Atomic service API: This API can be used in atomic services since API version 12.
showCloseboolean |ResourceNoWhether to show the close button.
Default value: true
Atomic service API: This API can be used in atomic services since API version 12.
onClose() => voidNoCallback for the popup close button.
Atomic service API: This API can be used in atomic services since API version 12.
buttons[PopupButtonOptions?,PopupButtonOptions?]NoButtons of the popup. A maximum of two buttons can be set.
Atomic service API: This API can be used in atomic services since API version 12.
direction12+DirectionNoLayout direction.
Default value: Direction.Auto
Atomic service API: This API can be used in atomic services since API version 12.
maxWidth18+DimensionNoMaximum width of the popup. This API allows you to display the popup with a custom width.
Default value: 400 vp
Atomic service API: This API can be used in atomic services since API version 18.

PopupTextOptions

Defines the text parameters of the popup.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
textResourceStrYesText content.
fontSizenumber |string |ResourceNoText font size.
Default value: $r('sys.float.ohos_id_text_size_body2')
The string value must be convertible to a number (for example, '10') or include a length unit (for example, '10px'); percentage-based strings are not supported.
Value range of number values: (0, +∞)
fontColorResourceColorNoText font color.
Default value: $r('sys.color.ohos_id_color_text_secondary')
fontWeightnumber |FontWeight |stringNoText font weight.
For the number type, the value ranges from 100 to 900, at an interval of 100. A larger value indicates a heavier font weight. The default value is 400.
For the string type, only strings of the number type are supported, for example, "400", "bold", "bolder", "lighter", "regular", and "medium", which correspond to the enumerated values in FontWeight.
Default value: FontWeight.Regular

PopupButtonOptions

Defines the button attributes and events.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
textResourceStrYesText of the button.
action() => voidNoClick callback of the button.
fontSizenumber |string |ResourceNoFont size of the button text.
Default value: $r('sys.float.ohos_id_text_size_button2')
fontColorResourceColorNoFont color of the button text.
Default value: $r('sys.color.ohos_id_color_text_primary_activated')

PopupIconOptions

Defines the attributes of the icon (in the upper left corner).

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
imageResourceStrYesIcon content.
widthDimensionNoIcon width.
Default value: 32VP
heightDimensionNoIcon height.
Default value: 32VP
fillColorResourceColorNoIcon fill color.
NOTE
This property applies only to an SVG image.
borderRadiusLength |BorderRadiusesNoRounded corner of the icon.
Default value: $r('sys.float.ohos_id_corner_radius_default_s')

Example

Example 1: Setting the Popup Style

This example demonstrates how to customize the style of a popup by configuring PopupIconOptions, PopupTextOptions, and PopupButtonOptions.

// xxx.ets
import { Popup, PopupTextOptions, PopupButtonOptions, PopupIconOptions } from '@kit.ArkUI';

@Entry
@Component
struct PopupExample {

  build() {
    Row() {
      // Define a popup.
      Popup({
        // Set the icon through PopupIconOptions.
        icon: {
          image: $r('app.media.icon'),
          width:32,
          height:32,
          fillColor:Color.White,
          borderRadius: 16
        } as PopupIconOptions,
        // Set the text through PopupTextOptions.
        title: {
          text: 'This is a popup with PopupOptions',
          fontSize: 20,
          fontColor: Color.Black,
          fontWeight: FontWeight.Normal
        } as PopupTextOptions,
        // Set the text through PopupTextOptions.
        message: {
          text: 'This is the message',
          fontSize: 15,
          fontColor: Color.Black
        } as PopupTextOptions,
        showClose: false,
        onClose: () => {
          console.info('close Button click')
        },
        // Set the button through PopupButtonOptions.
        buttons: [{
          text: 'confirm',
          action: () => {
            console.info('confirm button click')
          },
          fontSize: 15,
          fontColor: Color.Black,
        },
          {
            text: 'cancel',
            action: () => {
              console.info('cancel button click')
            },
            fontSize: 15,
            fontColor: Color.Black
          },] as [PopupButtonOptions?, PopupButtonOptions?]
      })
    }
    .width(300)
    .height(200)
    .borderWidth(2)
    .justifyContent(FlexAlign.Center)
  }
}

Example 2: Implementing a Mirror Effect

This example shows how to achieve a mirror effect for a popup by configuring direction.

// xxx.ets
import { Popup, PopupTextOptions, PopupButtonOptions, PopupIconOptions } from '@kit.ArkUI'

@Entry
@Component
struct PopupPage {
  @State currentDirection: Direction = Direction.Rtl

  build() {
    Column() {
      // Define a popup.
      Popup({
        // Set the icon through PopupIconOptions.
        direction: this.currentDirection,
        icon: {
          image: $r('app.media.icon'),
          width: 32,
          height: 32,
          fillColor: Color.White,
          borderRadius: 16,
        } as PopupIconOptions,
        // Set the text through PopupTextOptions.
        title: {
          text: 'This is a popup with PopupOptions',
          fontSize: 20,
          fontColor: Color.Black,
          fontWeight: FontWeight.Normal,

        } as PopupTextOptions,
        // Set the text through PopupTextOptions.
        message: {
          text: 'This is the message',
          fontSize: 15,
          fontColor: Color.Black,
        } as PopupTextOptions,
        showClose: true,
        onClose: () => {
          console.info('close Button click')
        },
        // Set the button through PopupButtonOptions.
        buttons: [{
          text: 'confirm',
          action: () => {
            console.info('confirm button click')
          },
          fontSize: 15,
          fontColor: Color.Black,

        },
          {
            text: 'cancel',
            action: () => {
              console.info('cancel button click')
            },
            fontSize: 15,
            fontColor: Color.Black,
          },] as [PopupButtonOptions?, PopupButtonOptions?],
      })

    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('100%')
  }
}

Example 3: Setting the Custom Width

This example demonstrates how to set the custom width for a popup using maxWidth.

// xxx.ets
import { Popup, PopupTextOptions, PopupButtonOptions, PopupIconOptions } from '@kit.ArkUI'

@Entry
@Component
struct PopupPage {
  @State currentDirection: Direction = Direction.Rtl

  build() {
     Row() {
       // Define a popup.
       Popup({
         // Set the custom width.
         maxWidth:'50%',
         // Set the icon through PopupIconOptions.
         icon: {
           image: $r('app.media.startIcon'),
           width: 32,
           height: 32,
           fillColor: Color.White,
           borderRadius: 16,
         } as PopupIconOptions,
         // Set the text through PopupTextOptions.
         message: {
           text: 'This is the message. This is the message. This is the message. This is the message.',
           fontSize: 15,
           fontColor: Color.Black
         } as PopupTextOptions,
         showClose: false,
         onClose: () => {
           console.info('close Button click')
         },
         // Set the button through PopupButtonOptions.
         buttons: [{
           text: 'OK',
           action: () => {
             console.info('confirm button click')
           },
           fontSize: 15,
           fontColor: Color.Black,
         },
           {
             text: 'Cancel',
             action: () => {
               console.info('cancel button click')
             },
             fontSize: 15,
             fontColor: Color.Black
           },] as [PopupButtonOptions?, PopupButtonOptions?]
       })
     }
     .width(400)
     .height(200)
     .borderWidth(2)
     .justifyContent(FlexAlign.Center)
   }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙advanced.Counter

harmony 鸿蒙Dialog Box (Dialog)

harmony 鸿蒙DialogV2

harmony 鸿蒙DownloadFileButton

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/qhTjw9