鸿蒙开发全局UI方法:【警告弹窗】

警告弹窗

显示警告弹窗组件,可设置文本内容与响应回调。

说明:

从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext]说明。

从API version 10开始,可以通过使用[UIContext]中的[showAlertDialog]来明确UI的执行上下文。

属性

名称参数类型参数描述
show[AlertDialogParamWithConfirm][AlertDialogParamWithButtons]

AlertDialogParamWithConfirm对象说明

参数名参数类型必填参数描述
title[ResourceStr]弹窗标题。
subtitle10+[ResourceStr]弹窗副标题。
message[ResourceStr]弹窗内容。
autoCancelboolean点击遮障层时,是否关闭弹窗。 默认值:true
confirm{ enabled10+?: boolean, defaultFocus10+?: boolean, style10+?: [DialogButtonStyle], value: [ResourceStr], fontColor?: [ResourceColor], backgroundColor?: [ResourceColor], action: () => void }确认按钮的使能状态、默认焦点、按钮风格、文本内容、文本色、按钮背景色和点击回调。 enabled:点击button是否响应。 默认值:true。 defaultFocus:设置button是否是默认焦点。 默认值:false。 style:设置button的风格样式。 默认值:DialogButtonStyle.DEFAULT。 value:按钮文本内容。 fontColor:按钮文本颜色。 backgroundColor:按钮背景颜色。 action: 按钮选中时的回调。
cancel() => void点击遮障层关闭dialog时的回调。
alignment[DialogAlignment]弹窗在竖直方向上的对齐方式。 默认值:DialogAlignment.Default
offset[Offset]弹窗相对alignment所在位置的偏移量。 默认值:{ dx: 0 , dy: 0 }
gridCountnumber弹窗容器宽度所占用栅格数。 默认值:4
maskRect10+[Rectangle]弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。 默认值:{ x: 0, y: 0, width: ‘100%’, height: ‘100%’ }

confirm参数优先级:fontColor、backgroundColor > style > defaultFocus

backgroundColorfontColorstyledefaultFocus效果
绿底红字--绿底红字
绿底-DialogButtonStyle.HIGHLIGHT-绿底白字
绿底-DialogButtonStyle.DEFAULT-绿底蓝字
绿底--TRUE绿底白字
绿底--FALSE/-绿底蓝字
-红字DialogButtonStyle.HIGHLIGHT-蓝底红字
-红字DialogButtonStyle.DEFAULT-白底红字
-红字-TRUE蓝底红字
-红字-FALSE/-白底红字
--DialogButtonStyle.HIGHLIGHT-蓝底白字
--DialogButtonStyle.DEFAULT-白底蓝字
---TRUE蓝底白字
---FALSE/-白底蓝字

AlertDialogParamWithButtons对象说明

参数名参数类型必填参数描述
title[ResourceStr]弹窗标题。
subtitle10+[ResourceStr]弹窗副标题。
message[ResourceStr]弹窗内容。
autoCancelboolean点击遮障层时,是否关闭弹窗。 默认值:true
primaryButton{ enabled10+?: boolean, defaultFocus10+?: boolean, style10+?: [DialogButtonStyle], value: [ResourceStr], fontColor?: [ResourceColor], backgroundColor?: [ResourceColor], action: () => void; }确认按钮的使能状态、默认焦点、按钮风格、文本内容、文本色、按钮背景色和点击回调。 enabled:点击button是否响应。 默认值:true。 defaultFocus:设置button是否是默认焦点。 默认值:false。 style:设置button的风格样式。 默认值:DialogButtonStyle.DEFAULT。 value:按钮文本内容。 fontColor:按钮文本颜色。 backgroundColor:按钮背景颜色。 action: 按钮选中时的回调。
secondaryButton{ enabled10+?: boolean, defaultFocus10+?: boolean, style10+?: [DialogButtonStyle], value: [ResourceStr], fontColor?: [ResourceColor], backgroundColor?: [ResourceColor], action: () => void; }确认按钮的使能状态、默认焦点、按钮风格、文本内容、文本色、按钮背景色和点击回调。 enabled:点击button是否响应。 默认值:true。 defaultFocus:设置button是否是默认焦点。 默认值:false。 style:设置button的风格样式。 默认值:DialogButtonStyle.DEFAULT。 value:按钮文本内容。 fontColor:按钮文本颜色。 backgroundColor:按钮背景颜色。 action: 按钮选中时的回调。
cancel() => void点击遮障层关闭dialog时的回调。
alignment[DialogAlignment]弹窗在竖直方向上的对齐方式。 默认值:DialogAlignment.Default
offset[Offset]弹窗相对alignment所在位置的偏移量。
gridCountnumber弹窗容器宽度所占用栅格数。
maskRect10+[Rectangle]弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。 默认值:{ x: 0, y: 0, width: ‘100%’, height: ‘100%’ }

AlertDialogParamWithOptions10+对象说明

参数名参数类型必填参数描述
title[ResourceStr]弹窗标题。
subtitle10+[ResourceStr]弹窗子标题。
message[ResourceStr]弹窗内容。
autoCancelboolean点击遮障层时,是否关闭弹窗。 默认值:true
cancel() => void点击遮障层关闭dialog时的回调。
alignment[DialogAlignment]弹窗在竖直方向上的对齐方式。 默认值:DialogAlignment.Default
offset[Offset]弹窗相对alignment所在位置的偏移量。
gridCountnumber弹窗容器宽度所占用栅格数。
maskRect10+[Rectangle]弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。 默认值:{ x: 0, y: 0, width: ‘100%’, height: ‘100%’ }
buttons10+Array<[AlertDialogButtonOptions]>弹窗容器中的多个按钮。
buttonDirection10+[DialogButtonDirection]按钮排布方向默认值为DialogButtonDirection.AUTO,建议3个以上按钮使用Auto模式(两个以上按钮会切换为纵向模式,通常能显示更多按钮),非Auto模式下,3个以上按钮可能会显示不全,超出显示范围的按钮会被截断。

AlertDialogButtonOptions10+对象说明

参数名参数类型必填参数描述
enabledboolean点击button是否响应,默认值true。
defaultFocusboolean设置button是否是默认焦点,默认值false。
style[DialogButtonStyle]设置button的风格样式,默认值DialogButtonStyle.DEFAULT。
value[ResourceStr]按钮的文本内容,若值为null,则该按钮不显示。
fontColor[ResourceColor]按钮的文本颜色。
backgroundColor[ResourceColor]按钮背景颜色。
action() => void按钮选中时的回调。

DialogButtonDirection10+枚举说明

名称描述
AUTO两个及以下按钮水平排布,两个以上为竖直排布。
HORIZONTAL按钮水平布局。
VERTICAL按钮竖直布局。

confirm参数优先级:fontColor、backgroundColor > style > defaultFocus

backgroundColorfontColorstyledefaultFocus效果
绿底红字--绿底红字
绿底-DialogButtonStyle.HIGHLIGHT-绿底白字
绿底-DialogButtonStyle.DEFAULT-绿底蓝字
绿底--TRUE绿底白字
绿底--FALSE/-绿底蓝字
-红字DialogButtonStyle.HIGHLIGHT-蓝底红字
-红字DialogButtonStyle.DEFAULT-白底红字
-红字-TRUE蓝底红字
-红字-FALSE/-白底红字
--DialogButtonStyle.HIGHLIGHT-蓝底白字
--DialogButtonStyle.DEFAULT-白底蓝字
---TRUE蓝底白字
---FALSE/-白底蓝字

DialogAlignment枚举说明

名称描述
Top垂直顶部对齐。
Center垂直居中对齐。
Bottom垂直底部对齐。
Default默认对齐。
TopStart8+左上对齐。
TopEnd8+右上对齐。
CenterStart8+左中对齐。
CenterEnd8+右中对齐。
BottomStart8+左下对齐。
BottomEnd8+右下对齐。

Rectangle10+类型说明

Rectangle是各种Dialog中maskRect参数的类型。

名称类型必填描述
x[Length]弹窗遮蔽层区域相对于窗口左上角的x轴坐标。 默认值:0vp
y[Length]弹窗遮蔽层区域相对于窗口左上角的y轴坐标。 默认值:0vp
width[Length]弹窗遮蔽层区域的宽度。 默认值:‘100%’
height[Length]弹窗遮蔽层区域的高度。 默认值:‘100%’

说明:

x和y可以设置正负值百分比。当x设置为’100%‘时表示遮蔽层区域往右偏移窗口本身宽度大小,当x设置为’-100%'时表示遮蔽层区域往左偏移窗口本身宽度大小。当y设置为’100%‘时表示遮蔽层区域往下偏移窗口本身高度大小,当y设置为’-100%'时表示遮蔽层区域往上偏移窗口本身高度大小。

width和height只能设置正值,支持百分比,如果设置为负值,那么该值将被重置为默认值。

百分比相对于窗口自身宽高进行计算。

DialogButtonStyle10+枚举说明

名称描述
DEFAULT白底蓝字(深色主题:白底=黑底)。
HIGHLIGHT蓝底白字。

示例

// xxx.ets
@Entry
@Component
struct AlertDialogExample {
  build() {
    Column({ space: 5 }) {
      Button('one button dialog')
        .onClick(() => {
          AlertDialog.show(
            {
              title: 'title',
              message: 'text',
              autoCancel: true,
              alignment: DialogAlignment.Bottom,
              offset: { dx: 0, dy: -20 },
              gridCount: 3,
              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',
              subtitle: 'subtitle',
              message: 'text',
              autoCancel: true,
              alignment: DialogAlignment.Bottom,
              gridCount: 4,
              offset: { dx: 0, dy: -20 },
              primaryButton: {
                value: 'cancel',
                action: () => {
                  console.info('Callback when the first button is clicked')
                }
              },
              secondaryButton: {
                enabled: true,
                defaultFocus: true,
                style: DialogButtonStyle.HIGHLIGHT,
                value: 'ok',
                action: () => {
                  console.info('Callback when the second button is clicked')
                }
              },
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
        }).backgroundColor(0x317aff)
        Button('three button dialog')
        .onClick(() => {
          AlertDialog.show(
            {
              title: 'title',
              subtitle: 'subtitle',
              message: 'text',
              autoCancel: true,
              alignment: DialogAlignment.Bottom,
              gridCount: 4,
              offset: { dx: 0, dy: -20 },
              buttonDirection: DialogButtonDirection.HORIZONTAL,
              buttons: [
                {
                  value: '按钮',
                  action: () => {
                    console.info('Callback when button1 is clicked')
                  }
                },
                {
                  value: '按钮',
                  action: () => {
                    console.info('Callback when button2 is clicked')
                  }
                },
                {
                  value: '按钮',
                  enabled: true,
                  defaultFocus: true,
                  style: DialogButtonStyle.HIGHLIGHT,
                  action: () => {
                    console.info('Callback when button3 is clicked')
                  }
                },
              ],
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

在最后

如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
更多鸿蒙最新技术知识点,请关注作者博客:https://gitee.com/li-shizhen-skin/zhihu/blob/master/README.md

  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值