ts-methods-custom-dialog-box.md 6.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, openAnimation?: AnimateParam, closeAniamtion?: AnimateParam, showInSubWindow?: boolean})
E
ester.zhou 已提交
15 16 17 18


**Parameters**

E
ester.zhou 已提交
19 20 21 22 23 24 25
| Name                   | Type                                    | Mandatory                 | Description                  |
| ---------------------- | ---------------------------------------- | ------------------------- | ---------------------- |
| builder                | [CustomDialog](../../quick-start/arkts-dynamic-ui-elememt-building.md#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.|
E
ester.zhou 已提交
26
| 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 已提交
27
| gridCount<sup>8+</sup> | number                                   | No                   | Number of [grid columns](../../ui/ui-ts-layout-grid-container-new.md) occupied by the dialog box.<br>The default value is 4, 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 已提交
28
| maskColor<sup>10+</sup>     | [ResourceColor](ts-types.md#resourcecolor)  | No  | Custom mask color.<br>Default value: **0x33000000**                                                                                                 |
E
ester.zhou 已提交
29
| openAnimation<sup>10+</sup> | [AnimateParam](ts-explicit-animation.md#animateparam)     | No  | Parameters for defining the open animation of the dialog box.<br>**NOTE**<br>If **iterations** is set to an odd number and **playMode** is set to **Reverse**, the dialog box will not be displayed when the animation ends.     |
E
ester.zhou 已提交
30
| closeAniamtion<sup>10+</sup>| [AnimateParam](ts-explicit-animation.md#animateparam)     | No  | Parameters for defining the close animation of the dialog box.     |
E
ester.zhou 已提交
31
| showInSubWindow<sup>10+</sup>| boolean     | No  | Whether to display a dialog box in a subwindow.<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**.     |
Z
zengyawen 已提交
32

33
## CustomDialogController
Z
zengyawen 已提交
34 35 36

### Objects to Import

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

44
### open()
Z
zengyawen 已提交
45 46
open(): void

Z
zengyawen 已提交
47 48 49

Opens the content of the custom dialog box. If the content has been displayed, this API does not take effect.

Z
zengyawen 已提交
50

51
### close
Z
zengyawen 已提交
52
close(): void
Z
zengyawen 已提交
53

E
ester.zhou 已提交
54

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

Z
zengyawen 已提交
57 58 59

## Example

60 61
```ts
// xxx.ets
Z
zengyawen 已提交
62 63
@CustomDialog
struct CustomDialogExample {
E
ester.zhou 已提交
64 65
  @Link textValue: string
  @Link inputValue: string
Z
zengyawen 已提交
66 67 68 69 70 71
  controller: CustomDialogController
  cancel: () => void
  confirm: () => void

  build() {
    Column() {
E
ester.zhou 已提交
72 73 74 75 76 77
      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 已提交
78 79 80 81 82 83 84 85
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            this.controller.close()
            this.cancel()
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
E
ester.zhou 已提交
86
            this.inputValue = this.textValue
Z
zengyawen 已提交
87 88 89 90 91 92 93 94 95 96 97
            this.controller.close()
            this.confirm()
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }
  }
}

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

E
ester.zhou 已提交
115 116 117 118 119
  aboutToDisappear() {
    delete this.dialogController,
    this.dialogController = undefined
  }

Z
zengyawen 已提交
120 121 122
  onCancel() {
    console.info('Callback when the first button is clicked')
  }
E
ester.zhou 已提交
123

Z
zengyawen 已提交
124 125 126
  onAccept() {
    console.info('Callback when the second button is clicked')
  }
E
ester.zhou 已提交
127

Z
zengyawen 已提交
128 129 130 131 132 133
  existApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
    Column() {
E
ester.zhou 已提交
134
      Button(this.inputValue)
Z
zengyawen 已提交
135
        .onClick(() => {
E
ester.zhou 已提交
136 137 138
          if (this.dialogController != undefined) {
            this.dialogController.open()
          }
Z
zengyawen 已提交
139 140 141 142 143 144
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
```

Z
zengyawen 已提交
145
![en-us_image_0000001212058470](figures/en-us_image_0000001212058470.gif)