ts-methods-custom-dialog-box.md 8.5 KB
Newer Older
Z
zengyawen 已提交
1 2
# Custom Dialog Box

E
ester.zhou 已提交
3
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.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6 7
> **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.
Z
zengyawen 已提交
8 9


E
ester.zhou 已提交
10 11


Z
zengyawen 已提交
12 13
## APIs

E
ester.zhou 已提交
14
CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean, alignment?: DialogAlignment, offset?: Offset, customStyle?: boolean, gridCount?: number, maskColor?: ResourceColor, maskRect?: Rectangle, openAnimation?: AnimateParam, closeAniamtion?: AnimateParam, showInSubWindow?: boolean, backgroundColor?:ResourceColor, cornerRadius?:Dimension \| BorderRadiuses})
E
ester.zhou 已提交
15 16 17

**Parameters**

E
ester.zhou 已提交
18 19 20 21 22 23 24 25
| Name                          | Type                                    | Mandatory  | Description                                    |
| ----------------------------- | ---------------------------------------- | ---- | ---------------------------------------- |
| builder                       | CustomDialog                             | Yes   | Constructor of the custom dialog box content.                             |
| cancel                        | () => void                  | No   | Callback invoked when the dialog box is closed after the overlay exits.                            |
| autoCancel                    | boolean                                  | No   | Whether to allow users to click the overlay to exit.<br>Default value: **true**                |
| alignment                     | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment) | No   | Alignment mode of the dialog box in the vertical direction.<br>Default value: **DialogAlignment.Default**|
| offset                        | [Offset](ts-types.md#offset)             | No   | Offset of the dialog box relative to the alignment position.                  |
| customStyle                   | boolean                                  | No   | Whether to use a custom style for the dialog box.<br>Default value: **false**, which means that the dialog box automatically adapts its width to the grid system and its height to the child components; the maximum height is 90% of the container height; the rounded corner is 24 vp.|
E
ester.zhou 已提交
26
| gridCount<sup>8+</sup>        | number                                   | No   | Number of [grid columns](../../ui/arkts-layout-development-grid-layout.md) occupied by the dialog box.<br>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.|
E
ester.zhou 已提交
27
| maskColor<sup>10+</sup>       | [ResourceColor](ts-types.md#resourcecolor) | No   | Custom mask color.<br>Default value: **0x33000000**             |
E
ester.zhou 已提交
28
| maskRect<sup>10+</sup>        | [Rectangle](ts-methods-alert-dialog-box.md#rectangle10) | No    | Mask area of the dialog box. Events outside the mask area are transparently transmitted, and events within the mask area are not.<br>Default value: **{ x: 0, y: 0, width: '100%', height: '100%' }**|
E
ester.zhou 已提交
29 30
| openAnimation<sup>10+</sup>   | [AnimateParam](ts-explicit-animation.md#animateparam) | No   | Parameters for defining the open animation of the dialog box.<br>**NOTE**<br>**iterations**: The default value is **1**, indicating that the animation is played once; any other value evaluates to the default value.<br>**playMode**: The default value is **PlayMode.Normal**; any other value evaluates to the default value.|
| closeAniamtion<sup>10+</sup>  | [AnimateParam](ts-explicit-animation.md#animateparam) | No   | Parameters for defining the close animation of the dialog box.<br>**NOTE**<br>**iterations**: The default value is **1**, indicating that the animation is played once; any other value evaluates to the default value.<br>**playMode**: The default value is **PlayMode.Normal**; any other value evaluates to the default value.                   |
E
ester.zhou 已提交
31 32 33
| showInSubWindow<sup>10+</sup> | boolean                                  | No   | Whether to show the dialog box in a sub-window when the dialog box needs to be displayed outside the main window.<br>Default value: **false**, indicating that the dialog box is not displayed in the subwindow<br>**NOTE**<br>A dialog box whose **showInSubWindow** attribute is **true** cannot trigger the display of another dialog box whose **showInSubWindow** attribute is also **true**.|
| backgroundColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor)      | No  | Background color of the dialog box.                                          |
| cornerRadius<sup>10+</sup>    | [BorderRadiuses](ts-types.md#borderradiuses9) \| [Dimension](ts-types.md#dimension10) | No  | Radius of the rounded corners of the background.<br>You can set separate radiuses for the four rounded corners.<br>Default value: **{ topLeft: '24vp', topRight: '24vp', bottomLeft: '24vp', bottomRight: '24vp' }**<br>**NOTE**<br>This attribute must be used together with the [borderRadius](ts-universal-attributes-border.md) attribute.|
Z
zengyawen 已提交
34

35
## CustomDialogController
Z
zengyawen 已提交
36 37 38

### Objects to Import

E
ester.zhou 已提交
39
```ts
Z
zengyawen 已提交
40 41
dialogController : CustomDialogController = new CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean})
```
E
ester.zhou 已提交
42
> **NOTE**
E
ester.zhou 已提交
43
>
E
ester.zhou 已提交
44
> **CustomDialogController** is valid 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.
Z
zengyawen 已提交
45

46
### open()
Z
zengyawen 已提交
47 48
open(): void

Z
zengyawen 已提交
49

E
ester.zhou 已提交
50
Opens the content of the custom dialog box. This API can be called multiple times. If the dialog box displayed in a subwindow, no new subwindow is allowed.
Z
zengyawen 已提交
51

Z
zengyawen 已提交
52

53
### close
Z
zengyawen 已提交
54
close(): void
Z
zengyawen 已提交
55

E
ester.zhou 已提交
56

57
Closes the custom dialog box. If the dialog box is closed, this API does not take effect.
Z
zengyawen 已提交
58

Z
zengyawen 已提交
59 60 61

## Example

62 63
```ts
// xxx.ets
Z
zengyawen 已提交
64 65
@CustomDialog
struct CustomDialogExample {
E
ester.zhou 已提交
66 67
  @Link textValue: string
  @Link inputValue: string
Z
zengyawen 已提交
68
  controller: CustomDialogController
E
ester.zhou 已提交
69
  // 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 at the end.
Z
zengyawen 已提交
70 71 72 73 74
  cancel: () => void
  confirm: () => void

  build() {
    Column() {
E
ester.zhou 已提交
75 76 77 78 79 80
      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('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
Z
zengyawen 已提交
81 82 83 84 85 86 87 88
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            this.controller.close()
            this.cancel()
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
E
ester.zhou 已提交
89
            this.inputValue = this.textValue
Z
zengyawen 已提交
90 91 92 93
            this.controller.close()
            this.confirm()
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
E
ester.zhou 已提交
94 95
    }.borderRadius(10)
    // When using the border or cornerRadius attribute, use it together with the borderRadius attribute.
Z
zengyawen 已提交
96 97 98 99 100 101
  }
}

@Entry
@Component
struct CustomDialogUser {
E
ester.zhou 已提交
102 103
  @State textValue: string = ''
  @State inputValue: string = 'click me'
Z
zengyawen 已提交
104
  dialogController: CustomDialogController = new CustomDialogController({
E
ester.zhou 已提交
105 106 107 108 109 110
    builder: CustomDialogExample({
      cancel: this.onCancel,
      confirm: this.onAccept,
      textValue: $textValue,
      inputValue: $inputValue
    }),
Z
zengyawen 已提交
111
    cancel: this.existApp,
E
ester.zhou 已提交
112 113 114 115
    autoCancel: true,
    alignment: DialogAlignment.Default,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
E
ester.zhou 已提交
116 117 118
    customStyle: false,
    backgroundColor: 0xd9ffffff,
    cornerRadius: 10,
Z
zengyawen 已提交
119 120
  })

E
ester.zhou 已提交
121
  // Delete the dialogController instance and set it to undefined when the custom component is about to be destroyed.
E
ester.zhou 已提交
122
  aboutToDisappear() {
E
ester.zhou 已提交
123
    delete this.dialogController, // Delete the dialogController instance.
E
ester.zhou 已提交
124
    this.dialogController = undefined //Set dialogController to undefined.
E
ester.zhou 已提交
125 126
  }

Z
zengyawen 已提交
127 128 129
  onCancel() {
    console.info('Callback when the first button is clicked')
  }
E
ester.zhou 已提交
130

Z
zengyawen 已提交
131 132 133
  onAccept() {
    console.info('Callback when the second button is clicked')
  }
E
ester.zhou 已提交
134

Z
zengyawen 已提交
135 136 137 138 139 140
  existApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
    Column() {
E
ester.zhou 已提交
141
      Button(this.inputValue)
Z
zengyawen 已提交
142
        .onClick(() => {
E
ester.zhou 已提交
143 144 145
          if (this.dialogController != undefined) {
            this.dialogController.open()
          }
Z
zengyawen 已提交
146 147 148 149 150 151
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
```

E
ester.zhou 已提交
152
![en-us_image_custom](figures/en-us_image_custom.gif)