ts-methods-alert-dialog-box.md 5.1 KB
Newer Older
Z
zengyawen 已提交
1
# Alert Dialog Box
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

4 5 6
> **NOTE**
>
> This method is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
7

Z
zengyawen 已提交
8

9
The **<AlertDialog\>** component is used to display an alert dialog box. You can set the text content and response callback for an alert dialog box as needed.
Z
zengyawen 已提交
10 11


Z
zengyawen 已提交
12
## Attributes
Z
zengyawen 已提交
13

14
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
15
| -------- | -------- | -------- | -------- |
16
| show | options: { paramObject1 \| paramObject2} | - | Defines and displays the **<AlertDialog\>** component. |
Z
zengyawen 已提交
17

Z
zengyawen 已提交
18
- paramObject1 parameters
E
esterzhou 已提交
19
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
20
  | -------- | -------- | -------- | -------- | -------- |
21 22
  | title | string \| [Resource](../../ui/ts-types.md) | No | - | Title of a dialog box. |
  | message | string \| [Resource](../../ui/ts-types.md) | Yes | - | Content of the dialog box. |
E
esterzhou 已提交
23
  | autoCancel | boolean | No | true | Whether to close the dialog box when the overlay is clicked. |
24
  | confirm | {<br/>value: string \| [Resource](../../ui/ts-types.md),<br>fontColor?: Color\| number \| string \| [Resource](../../ui/ts-types.md),<br/>backgroundColor?:Color \| number \| string \| [Resource](../../ui/ts-types.md),<br>action: () =&gt; void<br/>} <br/> | No | - | Text content, text color, background color, and click callback of the confirm button. |
E
esterzhou 已提交
25
  | cancel | () =&gt; void | No | - | Callback invoked when the dialog box is closed after the overlay is clicked. |
26 27
  | alignment | [DialogAlignment](ts-methods-custom-dialog-box.md) | No | DialogAlignment.Default | Alignment mode of the dialog box in the vertical direction. |
  | offset | {<br/>dx: Length \| [Resource](../../ui/ts-types.md),<br/>dy: Length \| [Resource](../../ui/ts-types.md)<br/>} | No | - | Offset of the dialog box relative to the alignment position. |
E
esterzhou 已提交
28
  | gridCount | number | No | - | Number of grid columns occupied by the width of the dialog box. |
Z
zengyawen 已提交
29

Z
zengyawen 已提交
30
- paramObject2 parameters
31
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
32
  | -------- | -------- | -------- | -------- | -------- |
33 34
  | title | string \| [Resource](../../ui/ts-types.md) | No | - | Title of a dialog box. |
  | message | string \| [Resource](../../ui/ts-types.md) | Yes | - | Content of the dialog box. |
E
esterzhou 已提交
35
  | autoCancel | boolean | No | true | Whether to close the dialog box when the overlay is clicked. |
36 37
  | primaryButton | {<br/>value: string \| [Resource](../../ui/ts-types.md),<br>fontColor?: Color \| number \| string \| [Resource](../../ui/ts-types.md),<br/>backgroundColor?:Color \| number\| string\| [Resource](../../ui/ts-types.md),<br>action: () =&gt; void<br/>} <br/> | No | - | Text content, text color, background color, and click callback of the primary button. |
  | secondaryButton | {<br/>value: string \|[Resource](../../ui/ts-types.md),<br>fontColor?: Color \| number \| string \| [Resource](../../ui/ts-types.md),<br/>backgroundColor?:Color \| number\| string\| [Resource](../../ui/ts-types.md),<br>action: () =&gt; void<br/>} <br/> | No | - | Text content, text color, background color, and click callback of the secondary button. |
E
esterzhou 已提交
38
  | cancel | () =&gt; void | No | - | Callback invoked when the dialog box is closed after the overlay is clicked. |
39 40
  | alignment | [DialogAlignment](ts-methods-custom-dialog-box.md) | No | DialogAlignment.Default | Alignment mode of the dialog box in the vertical direction. |
  | offset | {<br/>dx: Length \| [Resource](../../ui/ts-types.md),<br/>dy: Length  \| [Resource](../../ui/ts-types.md)<br/>} | No | - | Offset of the dialog box relative to the alignment position. |
E
esterzhou 已提交
41
  | gridCount | number | No | - | Number of grid columns occupied by the width of the dialog box. |
Z
zengyawen 已提交
42 43


Z
zengyawen 已提交
44
## Example
Z
zengyawen 已提交
45 46


47 48
```ts
// xxx.ets
Z
zengyawen 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
@Entry
@Component
struct AlertDialogExample {
  build() {
    Column({ space: 5 }) {
      Button('one button dialog')
        .onClick(() => {
          AlertDialog.show(
            {
              title: 'title',
              message: 'text',
              confirm: {
                value: 'button',
                action: () => {
                  console.info('Button-clicking callback')
                }
              },
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
      })
        .backgroundColor(0x317aff)
      Button('two button dialog')
        .onClick(() => {
          AlertDialog.show(
            {
              title: 'title',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  console.info('Callback when the first button is clicked')
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  console.info('Callback when the second button is clicked')
                }
              },
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
      }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
```

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