# 10.2:AlertDialog

ArkUI开发框架提供了AlertDialog组件来显示警告弹窗,本节笔者简单介绍下该组件的使用。

# 10.2.1:语法介绍

declare class AlertDialog {
    static show(value: AlertDialogParamWithConfirm | AlertDialogParamWithButtons | AlertDialogParamWithOptions);
}

declare interface AlertDialogParam {
    title?: ResourceStr;
    subtitle?: ResourceStr;
    message: ResourceStr;
    autoCancel?: boolean;
    cancel?: () => void;
    alignment?: DialogAlignment;
    offset?: Offset;
    gridCount?: number; 
    maskRect?: Rectangle;
    showInSubWindow?: boolean;
    isModal?: boolean;
    backgroundColor?: ResourceColor;
    backgroundBlurStyle?: BlurStyle;
    onWillDismiss?: Callback<DismissDialogAction>;
    transition?: TransitionEffect;
    cornerRadius?: Dimension | BorderRadiuses | LocalizedBorderRadiuses;
    width?: Dimension;
    height?: Dimension;
    borderWidth?: Dimension | EdgeWidths | LocalizedEdgeWidths;
    borderColor?: ResourceColor | EdgeColors | LocalizedEdgeColors;
    borderStyle?: BorderStyle | EdgeStyles;
    shadow?: ShadowOptions | ShadowStyle;
    textStyle?: TextStyle;
}

declare interface AlertDialogParamWithConfirm extends AlertDialogParam {
    confirm?: {
        enabled?: boolean;
        defaultFocus?: boolean;
        style?: DialogButtonStyle;
        value: ResourceStr;
        fontColor?: ResourceColor;        
        backgroundColor?: ResourceColor;
        action: () => void;
    };
}

declare interface AlertDialogParamWithButtons extends AlertDialogParam {
    primaryButton: {
        enabled?: boolean;
        defaultFocus?: boolean;
        style?: DialogButtonStyle;
        value: ResourceStr;
        fontColor?: ResourceColor;
        backgroundColor?: ResourceColor;
        action: () => void;
    };
    secondaryButton: {
        enabled?: boolean;
        defaultFocus?: boolean;
        style?: DialogButtonStyle;
        value: ResourceStr;
        fontColor?: ResourceColor;
        backgroundColor?: ResourceColor;
        action: () => void;
    };
}

declare interface AlertDialogParamWithOptions extends AlertDialogParam {
    buttons: Array<AlertDialogButtonOptions>;
    buttonDirection?: DialogButtonDirection;
}

declare interface AlertDialogButtonOptions {
    enabled?: boolean;
    defaultFocus?: boolean;
    style?: DialogButtonStyle;
    value: ResourceStr;
    fontColor?: ResourceColor;
    backgroundColor?: ResourceColor;
    action: () => void;
    primary?: boolean;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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

AlertDialog提供了一个静态方法show来显示一个警告对话框,它可以接收三种配置参数并继承自AlertDialogParam,各参数说明如下:

  • AlertDialogParamWithConfirm

  • AlertDialogParamWithButtons

  • AlertDialogParamWithOptions

import { promptAction } from '@kit.ArkUI';

@Entry
@ComponentV2
struct Index {

  build() {
    Column({space: 10}) {
      Button("AlertDialogParamWithConfirm")
        .onClick(() => {
          this.alertDialogParamWithConfirm()
        })
      Button("AlertDialogParamWithButtons")
        .onClick(() => {
          this.alertDialogParamWithButtons()
        })
      Button("AlertDialogParamWithOptions")
        .onClick(() => {
          this.alertDialogParamWithOptions()
        })
    }
    .width('100%')
  }

  private alertDialogParamWithConfirm() {
    let dialog: AlertDialogParamWithConfirm = {
      message: "弹窗内容",
      title: "弹窗标题",
      subtitle: "弹窗副标题",
      autoCancel: false,
      cancel: () => {
        promptAction.showToast({ message: "点击返回键" })
      },
      width: 260,
      backgroundColor: "#aabbcc",
      cornerRadius: 10,
      borderWidth: 2,
      borderColor: Color.Red,
      borderStyle: BorderStyle.Solid,
      backgroundBlurStyle: BlurStyle.Thin,
      confirm: {
        enabled: true,
        value: "按钮文字",
        backgroundColor: "#abcabc",
        fontColor: Color.Red,
        defaultFocus: true,
        action: () => {
          promptAction.showToast({ message: "点击了按钮" })
        }
      }
    }
    AlertDialog.show(dialog)
  }
  private alertDialogParamWithButtons() {
    let dialog: AlertDialogParamWithButtons = {
      message: "弹窗内容",
      title: "弹窗标题",
      subtitle: "弹窗副标题",
      autoCancel: false,
      cancel: () => {
        promptAction.showToast({ message: "点击返回键" })
      },
      backgroundColor: "#aabbcc",
      cornerRadius: 10,
      borderWidth: 2,
      borderColor: Color.Red,
      borderStyle: BorderStyle.Solid,
      backgroundBlurStyle: BlurStyle.Thin,
      primaryButton: {
        enabled: true,
        value: "第一个按钮",
        backgroundColor: "#abcabc",
        fontColor: Color.Red,
        defaultFocus: true,
        action: () => {
          promptAction.showToast({ message: "点击了第一个按钮" })
        }
      },
      secondaryButton: {
        enabled: true,
        value: "第二个按钮",
        backgroundColor: "#abcabc",
        fontColor: Color.Red,
        defaultFocus: true,
        action: () => {
          promptAction.showToast({ message: "点击了第二个按钮" })
        }
      }
    }
    AlertDialog.show(dialog)
  }

  private alertDialogParamWithOptions() {
    let buttons: Array<AlertDialogButtonOptions> = [
      {
        enabled: true,
        value: "第一个按钮",
        backgroundColor: "#abcabc",
        fontColor: Color.Red,
        defaultFocus: true,
        action: () => {
          promptAction.showToast({ message: "点击了第一个按钮" })
        }
      },
      {
        enabled: true,
        value: "第二个按钮",
        backgroundColor: "#abcabc",
        fontColor: Color.Red,
        defaultFocus: true,
        action: () => {
          promptAction.showToast({ message: "点击了第二个按钮" })
        }
      },
      {
        enabled: true,
        value: "第三个按钮",
        backgroundColor: "#abcabc",
        fontColor: Color.Red,
        defaultFocus: true,
        action: () => {
          promptAction.showToast({ message: "点击了第三个按钮" })
        }
      }
    ]
    let dialog: AlertDialogParamWithOptions = {
      buttons: buttons,
      message: "弹窗内容",
      title: "弹窗标题",
      subtitle: "弹窗副标题",
      autoCancel: false,
      cancel: () => {
        promptAction.showToast({ message: "点击返回键" })
      },
      backgroundColor: "#aabbcc",
      cornerRadius: 10,
      borderWidth: 2,
      borderColor: Color.Red,
      borderStyle: BorderStyle.Solid,
      backgroundBlurStyle: BlurStyle.Thin,
      isModal: false
    }
    AlertDialog.show(dialog)
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
请作者喝杯咖啡

津公网安备 12011402001367号

津ICP备2020008934号-2

中央网信办互联网违法和不良信息举报中心

天津市互联网违法和不良信息举报中心