• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 警告弹窗
2
3>  **说明:**
4> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
5
6
7显示警告弹窗组件,可设置文本内容与响应回调。
8
9
10## 属性
11
12| 名称 | 参数类型 | 默认值 | 参数描述 |
13| -------- | -------- | -------- | -------- |
14| show | value: { AlertDialogParamWithConfirm \| AlertDialogParamWithButtons} | - | 定义并显示AlertDialog组件。 |
15
16## AlertDialogParamWithConfirm对象说明
17| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
18| -------- | -------- | -------- | -------- | -------- |
19| title | string \| [Resource](ts-types.md#resource) | 否 | - | 弹窗标题。 |
20| message | string \| [Resource](ts-types.md#resource) | 是 | - | 弹窗内容。 |
21| autoCancel | boolean | 否 | true | 点击遮障层时,是否关闭弹窗。 |
22| confirm | {<br/>value:&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>fontColor?:&nbsp;Color&nbsp;\|&nbsp;number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>backgroundColor?:&nbsp;Color&nbsp;\|&nbsp;number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>action:&nbsp;()&nbsp;=&gt;&nbsp;void<br/>} | 否 | - | 确认按钮的文本内容、文本色、按钮背景色和点击回调。 |
23| cancel | ()&nbsp;=&gt;&nbsp;void | 否 | - | 点击遮障层关闭dialog时的回调。 |
24| alignment | [DialogAlignment](ts-methods-custom-dialog-box.md#dialogalignment枚举说明) | 否 | DialogAlignment.Default | 弹窗在竖直方向上的对齐方式。 |
25| offset | {<br/>dx:&nbsp;Length&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>dy:&nbsp;Length&nbsp;&nbsp;\|&nbsp;[Resource](ts-types.md#resource)<br/>} | 否 | - | 弹窗相对alignment所在位置的偏移量。 |
26| gridCount | number | 否 | - | 弹窗容器宽度所占用栅格数。<br/>**说明:**<br/>当gridCount小于等于0时,弹窗宽度是固定的;大于0时,按照设置的数值显示宽度,最大值为4,若值为小数,则向下取整。 |
27
28## AlertDialogParamWithButtons对象说明
29| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
30| -------- | -------- | -------- | -------- | -------- |
31| title | string&nbsp;\|&&nbsp;[Resource](ts-types.md#resource) | 否 | - | 弹窗标题。 |
32| message | string&nbsp;\|&&nbsp;[Resource](ts-types.md#resource) | 是 | - | 弹窗内容。 |
33| autoCancel | boolean | 否 | true | 点击遮障层时,是否关闭弹窗。 |
34| primaryButton | {<br/>value:&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>fontColor?:&nbsp;Color&nbsp;\|&nbsp;number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>backgroundColor?:&nbsp;Color&nbsp;\|&nbsp;number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>action:&nbsp;()&nbsp;=&gt;&nbsp;void;<br/>} | 否 | - | 按钮的文本内容、文本色、按钮背景色和点击回调。 |
35| secondaryButton | {<br/>value:&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>fontColor?:&nbsp;Color&nbsp;\|&nbsp;number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>backgroundColor?:&nbsp;Color&nbsp;\|&nbsp;number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>action:&nbsp;()&nbsp;=&gt;&nbsp;void;<br/>} | 否 | - | 按钮的文本内容、文本色、按钮背景色和点击回调。 |
36| cancel | ()&nbsp;=&gt;&nbsp;void | 否 | - | 点击遮障层关闭dialog时的回调。 |
37| alignment | [DialogAlignment](ts-methods-custom-dialog-box.md#dialogalignment枚举说明) | 否 | DialogAlignment.Default | 弹窗在竖直方向上的对齐方式。 |
38| offset | {<br/>dx:&nbsp;Length&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>dy:&nbsp;Length&nbsp;&nbsp;\|&nbsp;[Resource](ts-types.md#resource)<br/>} | 否 | - | 弹窗相对alignment所在位置的偏移量。 |
39| gridCount | number | 否 | - | 弹窗容器宽度所占用栅格数。 |
40
41
42## 示例
43
44```ts
45// xxx.ets
46@Entry
47@Component
48struct AlertDialogExample {
49  build() {
50    Column({ space: 5 }) {
51      Button('one button dialog')
52        .onClick(() => {
53          AlertDialog.show(
54            {
55              title: 'title',
56              message: 'text',
57              confirm: {
58                value: 'button',
59                action: () => {
60                  console.info('Button-clicking callback')
61                }
62              },
63              cancel: () => {
64                console.info('Closed callbacks')
65              }
66            }
67          )
68      })
69        .backgroundColor(0x317aff)
70      Button('two button dialog')
71        .onClick(() => {
72          AlertDialog.show(
73            {
74              title: 'title',
75              message: 'text',
76              primaryButton: {
77                value: 'cancel',
78                action: () => {
79                  console.info('Callback when the first button is clicked')
80                }
81              },
82              secondaryButton: {
83                value: 'ok',
84                action: () => {
85                  console.info('Callback when the second button is clicked')
86                }
87              },
88              cancel: () => {
89                console.info('Closed callbacks')
90              }
91            }
92          )
93      }).backgroundColor(0x317aff)
94    }.width('100%').margin({ top: 5 })
95  }
96}
97```
98
99![zh-cn_image_0000001174582844](figures/zh-cn_image_0000001174582844.gif)
100