harmony 鸿蒙Custom Dialog Box (CustomDialog)

  • 2025-06-12
  • 浏览 (2)

Custom Dialog Box (CustomDialog)

A custom dialog box is a dialog box you customize by using APIs of the CustomDialogController class. You can set the style and content to your preference for a custom dialog box.

NOTE

The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.

APIs

constructor(value: CustomDialogControllerOptions)

Defines a custom dialog box.

NOTE

No parameters of the custom dialog box can be dynamically updated.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
value CustomDialogControllerOptions Yes Parameters of the custom dialog box.

CustomDialogControllerOptions

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
builder CustomDialog Yes Builder of the custom dialog box content.
NOTE
If the builder uses a callback as the input parameter, as in build: custombuilder({ callback: ()=> {…}}), pay attention to the binding of this.
To listen for data changes in the builder, use the @Link decorator; other decorators, such as @Prop and @ObjectLink, do not apply.
Atomic service API: This API can be used in atomic services since API version 11.
cancel () => void No Callback invoked when the dialog box is closed after the Back button or mask is touched or the Esc key is pressed.
Atomic service API: This API can be used in atomic services since API version 11.
autoCancel boolean No Whether to close the dialog box when the mask is touched. The value true means to close the dialog box when the mask is touched, and false means the opposite.
Default value: true
Atomic service API: This API can be used in atomic services since API version 11.
alignment DialogAlignment No Alignment mode of the dialog box in the vertical direction.
Default value: DialogAlignment.Default
Atomic service API: This API can be used in atomic services since API version 11.
offset Offset No Offset of the dialog box relative to the alignment position.
Default value: {dx: 0, dy: 0}
Atomic service API: This API can be used in atomic services since API version 11.
customStyle boolean No Whether to use a custom style for the dialog box.
When this parameter is set to false (default value):
1. The rounded corner radius is 32 vp.
2. If the width and height of the dialog box are not set, the dialog box automatically adapts its width to the grid system and its height to the child components.
3. The set width of the dialog box cannot exceed the maximum width in the default style (100% width for a custom node), and the set height cannot exceed the maximum height (100% height for a custom node).
When this parameter is set to true:
1. The corner radius is 0, and the background color is transparent.
2. The width, height, border width, border style, border color, and shadow width cannot be set for the dialog box.
Atomic service API: This API can be used in atomic services since API version 11.
gridCount8+ number No Number of grid columns occupied by the dialog box.
The default value is subject to the window size, and the maximum value is the maximum number of columns supported by the system. If this parameter is set to an invalid value, the default value is used.
Value range: an integer no less than 0
Atomic service API: This API can be used in atomic services since API version 11.
maskColor10+ ResourceColor No Custom mask color.
Default value: 0x33000000
Atomic service API: This API can be used in atomic services since API version 11.
maskRect10+ Rectangle No Mask area of the dialog box. Events outside the mask area are transparently transmitted, and events within the mask area are not.
Default value: { x: 0, y: 0, width: ‘100%’, height: ‘100%’ }
NOTE
maskRect does not take effect when showInSubWindow is set to true.
Atomic service API: This API can be used in atomic services since API version 11.
openAnimation10+ AnimateParam No Parameters for defining the open animation of the dialog box.
NOTE
tempo: The default value is 1; a value less than or equal to 0 is handled as the default value.
iterations: The default value is 1, indicating that the animation is played once; any other value is handled as the default value.
playMode: The default value is PlayMode.Normal; any other value is handled as the default value.
Atomic service API: This API can be used in atomic services since API version 11.
closeAnimation10+ AnimateParam No Parameters for defining the close animation of the dialog box.
NOTE
tempo: The default value is 1; a value less than or equal to 0 is handled as the default value.
iterations: The default value is 1, indicating that the animation is played once; any other value is handled as the default value.
playMode: The default value is PlayMode.Normal; any other value is handled as the default value.
For page transition, you are advised to use the default close animation.
Atomic service API: This API can be used in atomic services since API version 11.
showInSubWindow10+ boolean No Whether to show the dialog box in a subwindow when the dialog box needs to be displayed outside the main window.
Default value: false
NOTE
A dialog box whose showInSubWindow attribute is true cannot trigger the display of another dialog box whose showInSubWindow attribute is also true. Avoid using the CalendarPicker, CalendarPickerDialog, DatePickerDialog, TextPickerDialog, and TimePickerDialog components in the dialog box where showInSubWindow is set to true, as the dialog box may affect the behavior of these components.
Atomic service API: This API can be used in atomic services since API version 11.
backgroundColor10+ ResourceColor No Background color of the dialog box.
Default value: Color.Transparent
NOTE
If the content builder also has the background color set, the background color set here will be overridden by the background color of the content builder.
When backgroundColor is set to a non-transparent color, backgroundBlurStyle must be set to BlurStyle.NONE; otherwise, the color display may not meet the expected effect.
Atomic service API: This API can be used in atomic services since API version 11.
cornerRadius10+ Dimension |BorderRadiuses No Radius of the rounded corners of the background.
You can set separate radiuses for the four rounded corners.
Default value: { topLeft: ‘32vp’, topRight: ‘32vp’, bottomLeft: ‘32vp’, bottomRight: ‘32vp’ }
NOTE
This attribute must be used together with the borderRadius attribute.
Atomic service API: This API can be used in atomic services since API version 11.
isModal11+ boolean No Whether the dialog box is a modal. A modal dialog box has a mask applied, while a non-modal dialog box does not.
Default value: true
Atomic service API: This API can be used in atomic services since API version 12.
onWillDismiss12+ Callback<DismissDialogAction> No Callback for interactive closure of the dialog box.
NOTE
1. If this callback is registered, the dialog box will not be closed immediately after the user touches the mask or the Back button, presses the Esc key, or swipes left or right on the screen. The reason parameter in the callback is used to determine whether the dialog box can be closed. The reason returned by the component does not support the value CLOSE_BUTTON.
2. In the onWillDismiss callback, another onWillDismiss callback is not allowed.
Atomic service API: This API can be used in atomic services since API version 12.
borderWidth12+ Dimension |EdgeWidths No Border width of the dialog box.
You can set the width for all four sides or set separate widths for individual sides.
Default value: 0
When set to a percentage, the value defines the border width as a percentage of the parent dialog box’s width.
If the left and right borders are greater than its width, or the top and bottom borders are greater than its height, the dialog box may not display as expected.
Atomic service API: This API can be used in atomic services since API version 12.
borderColor12+ ResourceColor |EdgeColors No Border color of the dialog box.
Default value: Color.Black
borderColor must be used with borderWidth in pairs.
Atomic service API: This API can be used in atomic services since API version 12.
borderStyle12+ BorderStyle |EdgeStyles No Border style of the dialog box.
Default value: BorderStyle.Solid
borderStyle must be used with borderWidth in pairs.
Atomic service API: This API can be used in atomic services since API version 12.
width12+ Dimension No Width of the dialog box.
NOTE
- Default maximum width of the dialog box: 400 vp
- When this parameter is set to a percentage, the reference width of the dialog box is the width of the window where the dialog box is located. You can decrease or increase the width as needed.
Atomic service API: This API can be used in atomic services since API version 12.
height12+ Dimension No Height of the dialog box.
NOTE
- Default maximum height of the dialog box: 0.9 x (Window height – Safe area)
- When this parameter is set to a percentage, the reference height of the dialog box is the height of the window where the dialog box is located minus the safe area. You can decrease or increase the height as needed.
Atomic service API: This API can be used in atomic services since API version 12.
shadow12+ ShadowOptions |ShadowStyle No Shadow of the dialog box.
Default value on 2-in-1 devices: ShadowStyle.OUTER_FLOATING_MD when the dialog box is focused and ShadowStyle.OUTER_FLOATING_SM otherwise
Atomic service API: This API can be used in atomic services since API version 12.
backgroundBlurStyle12+ BlurStyle No Background blur style of the dialog box.
Default value: BlurStyle.COMPONENT_ULTRA_THICK
NOTE
Setting this parameter to BlurStyle.NONE disables the background blur. When backgroundBlurStyle is set to a value other than NONE, do not set backgroundColor. If you do, the color display may not produce the expected visual effect.
Atomic service API: This API can be used in atomic services since API version 12.
backgroundBlurStyleOptions18+ BackgroundBlurStyleOptions No Options for customizing the background blur style.
Atomic service API: This API can be used in atomic services since API version 18.
backgroundEffect18+ BackgroundEffectOptions No Options for customizing the background effect.
Atomic service API: This API can be used in atomic services since API version 18.
keyboardAvoidMode12+ KeyboardAvoidMode No How the dialog box avoids the soft keyboard when it is brought up.
Default value: KeyboardAvoidMode.DEFAULT
Atomic service API: This API can be used in atomic services since API version 12.
enableHoverMode14+ boolean No Whether to enable the hover state.
Default value: false, meaning not to enable the hover state.
Atomic service API: This API can be used in atomic services since API version 14.
hoverModeArea14+ HoverModeAreaType No Display area of the dialog box in the hover state.
Default value: HoverModeAreaType.BOTTOM_SCREEN
Atomic service API: This API can be used in atomic services since API version 14.
onWillAppear18+ Callback&lt;void&gt; No Event callback when the dialog box is about to appear.
NOTE
1. The normal timing sequence is as follows: onWillAppear > onDidAppear > onWillDisappear > onDidDisappear.
2. You can set the callback event for changing the dialog box display effect in onWillAppear. The settings take effect next time the dialog box appears.
Atomic service API: This API can be used in atomic services since API version 18.
onDidAppear18+ Callback&lt;void&gt; No Event callback when the dialog box appears.
NOTE
1. The normal timing sequence is as follows: onWillAppear > onDidAppear > onWillDisappear > onDidDisappear.
2. You can set the callback event for changing the dialog box display effect in onDidAppear. The settings take effect next time the dialog box appears.
3. If the dialog is dismissed before its entrance animation is finished, the animation will be interrupted, and onDidAppear will not be triggered.
Atomic service API: This API can be used in atomic services since API version 18.
onWillDisappear18+ Callback&lt;void&gt; No Event callback when the dialog box is about to disappear.
NOTE
1. The normal timing sequence is as follows: onWillAppear > onDidAppear > onWillDisappear > onDidDisappear.
Atomic service API: This API can be used in atomic services since API version 18.
onDidDisappear18+ Callback&lt;void&gt; No Event callback when the dialog box disappears.
NOTE
1. The normal timing sequence is as follows: onWillAppear > onDidAppear > onWillDisappear > onDidDisappear.
Atomic service API: This API can be used in atomic services since API version 18.
keyboardAvoidDistance15+ LengthMetrics No Distance between the dialog box and the keyboard after keyboard avoidance is applied.
NOTE
- Default value: 16vp
- Default unit: vp
- This parameter takes effect only when keyboardAvoidMode is set to DEFAULT.
Atomic service API: This API can be used in atomic services since API version 15.
levelMode15+ LevelMode No Display level of the dialog box.
NOTE
- Default value: LevelMode.OVERLAY.
- This parameter takes effect only when showInSubWindow is set to false.
Atomic service API: This API can be used in atomic services since API version 15.
levelUniqueId15+ number No Unique ID of the node under the display level for the page-level dialog box.
NOTE
- This parameter takes effect only when levelMode is set to LevelMode.EMBEDDED.
Atomic service API: This API can be used in atomic services since API version 15.
immersiveMode15+ ImmersiveMode No Overlay effect for the page-level dialog box.
NOTE
- Default value: ImmersiveMode.DEFAULT
- This parameter takes effect only when levelMode is set to LevelMode.EMBEDDED.
Atomic service API: This API can be used in atomic services since API version 15.
levelOrder18+ LevelOrder No Display order of the dialog box.
NOTE
- Default value: LevelOrder.clamp(0)
- Dynamic updating is not supported.
Atomic service API: This API can be used in atomic services since API version 18.
focusable18+ boolean No Whether the dialog box can gain focus.
Default value: true
NOTE
Only dialog boxes that are displayed on top of the current window can gain focus.
Atomic service API: This API can be used in atomic services since API version 18.

NOTE

  • Pressing the Back or ESC key closes the dialog box.

  • If the dialog box reaches its maximum allowable height on the screen when avoiding the soft keyboard, it reduces its height to fit. It should be noted that this height adjustment is applied to the outermost container. If a child component within this container has been assigned a larger fixed height, since the container does not clip its content by default, parts of the dialog box may still be displayed off-screen.

  • Use the custom dialog box to contain simple alert messages only. Do not use it as a page. When the dialog box avoids the soft keyboard, there is a 16 vp safe spacing between the two.

  • Currently, dialog boxes in ArkUI do not close automatically when you switch pages unless you manually call close. To enable a dialog box to be covered during page navigation, consider using the Navigation component. For details, see the page display mode: dialog mode.

DismissDialogAction12+

Provides information about the action to dismiss the dialog box.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Properties

Name Type Readable Writable Description
dismiss Callback&lt;void&gt; No No Callback for dismissing the dialog box. This API is called only when the dialog box needs to be exited.
reason DismissReason No No Reason why the dialog box cannot be dismissed. You must specify whether to close the dialog box for each of the listed actions.

CustomDialogController

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

Objects to Import

dialogController : CustomDialogController|null = new CustomDialogController(CustomDialogControllerOptions)

NOTE

CustomDialogController is effective only when it is a member variable of the @CustomDialog and @Component decorated struct and is defined in the @Component decorated struct. For details, see the following example.

constructor

constructor(value: CustomDialogControllerOptions)

Constructor for a custom dialog box.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
value CustomDialogControllerOptions Yes Parameters of the custom dialog box.

open

open()

Opens the content of the custom dialog box. This API can be called multiple times. If the dialog box is displayed in a subwindow, no new subwindow is allowed.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

close

close()

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Closes the custom dialog box. If the dialog box is closed, this API does not take effect.

Example

Example 1: Opening Nested Dialog Boxes

This example demonstrates how to open one or more custom dialog boxes within another custom dialog box.

// xxx.ets
@CustomDialog
struct CustomDialogExampleTwo {
  controllerTwo?: CustomDialogController
  build() {
    Column() {
      Text('I'm the second dialog box')
        .fontSize(30)
        .height(100)
      Button('Close Second Dialog Box')
        .onClick(() => {
          if (this.controllerTwo != undefined) {
            this.controllerTwo.close()
          }
        })
        .margin(20)
    }
  }
}
@CustomDialog
@Component
struct CustomDialogExample {
  @Link textValue: string
  @Link inputValue: string
  dialogControllerTwo: CustomDialogController|null = new CustomDialogController({
    builder: CustomDialogExampleTwo(),
    alignment: DialogAlignment.Bottom,
    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
      console.log("dialog onWillDismiss")
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss()
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss()
      }
    },
    offset: { dx: 0, dy: -25 } })
  controller?: CustomDialogController
  // You can pass in multiple other controllers in the CustomDialog to open one or more other CustomDialogs in the CustomDialog. In this case, you must place the controller pointing to the self behind all controllers.
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }

  build() {
    Column() {
      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
        .onChange((value: string) => {
          this.textValue = value
        })
      Text('Are you sure you want to change the text?').fontSize(16).margin({ bottom: 10 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('No')
          .onClick(() => {
            if (this.controller != undefined) {
              this.controller.close()
              this.cancel()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('OK')
          .onClick(() => {
            if (this.controller != undefined) {
              this.inputValue = this.textValue
              this.controller.close()
              this.confirm()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })

      Button('Open Second Dialog Box')
        .onClick(() => {
          if (this.dialogControllerTwo != null) {
            this.dialogControllerTwo.open()
          }
        })
        .margin(20)
    }.borderRadius(10)
    // When using the border or cornerRadius attribute, use it together with the borderRadius attribute.
  }
}
@Entry
@Component
struct CustomDialogUser {
  @State textValue: string = ''
  @State inputValue: string = 'click me'
  dialogController: CustomDialogController|null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() },
      textValue: $textValue,
      inputValue: $inputValue
    }),
    cancel: this.exitApp,
    autoCancel: true,
    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
      console.log("dialog onWillDismiss")
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss()
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss()
      }
    },
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false,
    cornerRadius: 10,
  })

  // Set dialogController to null when the custom component is about to be destroyed.
  aboutToDisappear() {
    this.dialogController = null // Set dialogController to null.
  }

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  exitApp() {
    console.info('Click the callback in the blank area')
  }
  build() {
    Column() {
      Button(this.inputValue)
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}

en-us_image_custom

Example 2: Opening a Dialog Box Outside the Main Window

This example demonstrates how to configure a dialog box to display outside the main window on a 2-in-1 device by setting showInSubWindow to true.

// xxx.ets
@CustomDialog
struct CustomDialogExample {
  controller?: CustomDialogController
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }
  build() {
    Column() {
      Text('Dialog box outside the main window')
        .fontSize(30)
        .height(100)
      Button('Close')
        .onClick(() => {
          if (this.controller != undefined) {
            this.controller.close()
          }
        })
        .margin(20)
    }
  }
}
@Entry
@Component
struct CustomDialogUser {
  dialogController: CustomDialogController|null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() }
    }),
    cancel: this.existApp,
    autoCancel: true,
    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
      console.log("dialog onWillDismiss")
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss()
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss()
      }
    },
    alignment: DialogAlignment.Center,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    showInSubWindow: true,
    isModal: true,
    customStyle: false,
    cornerRadius: 10,
    focusable: true
  })
  // Set dialogController to null when the custom component is about to be destroyed.
  aboutToDisappear() {
    this.dialogController = null // Set dialogController to null.
  }

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  existApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
    Column() {
      Button('Click Me')
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}

en-us_image_custom-showinsubwindow

Example 3: Setting the Dialog Box Style

This example demonstrates how to set styles of a custom dialog box, including the width, height, background color, and shadow.

// xxx.ets
@CustomDialog
struct CustomDialogExample {
  controller?: CustomDialogController
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }
  build() {
    Column() {
      Text('This is a custom dialog box')
        .fontSize(30)
        .height(100)
      Button('Close')
        .onClick(() => {
          if (this.controller != undefined) {
            this.controller.close()
          }
        })
        .margin(20)
    }
  }
}
@Entry
@Component
struct CustomDialogUser {
  dialogController: CustomDialogController|null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() }
    }),
    cancel: this.existApp,
    autoCancel: true,
    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
      console.log("dialog onWillDismiss")
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss()
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss()
      }
    },
    alignment: DialogAlignment.Center,
    offset: { dx: 0, dy: -20 },
    customStyle: false,
    cornerRadius: 20,
    width: 300,
    height: 200,
    borderWidth: 1,
    borderStyle: BorderStyle.Dashed,// borderStyle must be used with borderWidth in pairs.
    borderColor: Color.Blue,// borderColor must be used with borderWidth in pairs.
    backgroundColor: Color.White,
    shadow: ({ radius: 20, color: Color.Grey, offsetX: 50, offsetY: 0}),
  })
  // Set dialogController to null when the custom component is about to be destroyed.
  aboutToDisappear() {
    this.dialogController = null // Set dialogController to null.
  }

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  existApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
    Column() {
      Button('Click Me')
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}

en-us_image_custom_style

Example 4: Configuring a Dialog Box in the Hover State

This example demonstrates how to set the layout area of a dialog box in the hover state on a foldable device.

@CustomDialog
@Component
struct CustomDialogExample {
  @Link textValue: string;
  @Link inputValue: string;
  controller?: CustomDialogController;

  build() {
    Column() {
      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
        .onChange((value: string) => {
          this.textValue = value;
        })
      Text('Are you sure you want to change the text?').fontSize(16).margin({ bottom: 10 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('No')
          .onClick(() => {
            if (this.controller != undefined) {
              this.controller.close();
            }
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('OK')
          .onClick(() => {
            if (this.controller != undefined) {
              this.inputValue = this.textValue;
              this.controller.close();
            }
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })

      Button('Open Second Dialog Box')
        .margin(20)
    }.borderRadius(10)
    // When using the border or cornerRadius attribute, use it together with the borderRadius attribute.
  }
}
@Entry
@Component
struct CustomDialogUser {
  @State textValue: string = '';
  @State inputValue: string = 'click me';
  dialogController: CustomDialogController|null = new CustomDialogController({
    builder: CustomDialogExample({
      textValue: $textValue,
      inputValue: $inputValue
    }),
    cancel: this.exitApp,
    autoCancel: true,
    onWillDismiss: (dismissDialogAction: DismissDialogAction)=> {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason));
      console.log("dialog onWillDismiss");
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss();
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss();
      }
    },
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false,
    cornerRadius: 10,
    enableHoverMode: true,
    hoverModeArea: HoverModeAreaType.TOP_SCREEN
  })

  // Set dialogController to null when the custom component is about to be destroyed.
  aboutToDisappear() {
    this.dialogController = null; // Set dialogController to null.
  }

  exitApp() {
    console.info('Click the callback in the blank area');
  }

  build() {
    Column() {
      Button(this.inputValue)
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open();
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}

en-us_image_custom

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙advanced.Counter

harmony 鸿蒙Dialog Box (Dialog)

harmony 鸿蒙DialogV2

harmony 鸿蒙DownloadFileButton

0  赞