HarmonyOS NEXT鸿蒙开发入门,警告弹窗 (AlertDialog)

1. 官方文档指南

  1. 警告弹窗 (AlertDialog) https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-alert-dialog-box-V5#alertdialogshow

2. 描述

定义警告弹窗并弹出。

3. 使用说明

AlertDialog.show

static show(value: AlertDialogParamWithConfirm | AlertDialogParamWithButtons | AlertDialogParamWithOptions)
static 修饰的方法,直接通过类名.方法的形式调用

  1. 参数:
参数名类型必填描述
valueAlertDialogParamWithConfirm
AlertDialogParamWithButtons
AlertDialogParamWithOptions
定义并显示AlertDialog组件

AlertDialogParam对象说明

参数名类型必填描述
titleResourceStr弹窗标题
subtitleResourceStr弹窗副标题
messageResourceStr弹窗内容
autoCancelboolean点击遮障层时,是否关闭弹窗,
true表示关闭弹窗。false表示不关闭弹窗。默认值:true
cancel() => void点击遮障层关闭dialog时的回调
alignmentDialogAlignment弹窗在竖直方向上的对齐方式 (Top Center Bottom)等

这里我只列举了常用的属性,更多使用方法请查看官方API文档

AlertDialogParamWithConfirm对象说明
继承自AlertDialogParam

参数名类型必填描述
confirm{enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: DialogButtonStyle,
value: ResourceStr,
fontColor?: ResourceColor,
backgroundColor?: ResourceColor,
action: () => void}
确认Button的使能状态、默认焦点、按钮风格、文本内容、文本色、按钮背景色和点击回调

AlertDialogParamWithButtons对象说明
继承自AlertDialogParam

参数名类型必填描述
primaryButton{enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: DialogButtonStyle,
value: ResourceStr,
fontColor?: ResourceColor,
backgroundColor?: ResourceColor,
action: () => void;}
确认Button的使能状态、默认焦点、按钮风格、文本内容、文本色、按钮背景色和点击回调。在弹窗获焦且未进行tab键走焦时,该按钮默认响应Enter键,且多重弹窗可自动获焦连续响应。默认响应Enter键能力在defaultFocus为true时不生效。
enabled:点击Button是否响应。
默认值:true
defaultFocus:设置Button是否是默认焦点。
默认值:false
style:设置Button的风格样式。
默认值:DialogButtonStyle.DEFAULT。
value:Button文本内容。
fontColor:Button文本颜色。
backgroundColor:Button背景颜色。
action: Button选中时的回调。
secondaryButton{enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: DialogButtonStyle,
value: ResourceStr,
fontColor?: ResourceColor,
backgroundColor?: ResourceColor,
action: () => void;}
确认Button的使能状态、默认焦点、按钮风格、文本内容、文本色、按钮背景色和点击回调。
enabled:点击Button是否响应。
默认值:true。
defaultFocus:设置Button是否是默认焦点。
默认值:false。
style:设置Button的风格样式。
默认值:DialogButtonStyle.DEFAULT。
value:Button文本内容。
fontColor:Button文本颜色。
backgroundColor:Button背景颜色。
action: Button选中时的回调。

在实际开发中,可以使用primaryButton和secondaryButton来设置【确认】和【取消】按钮

4. 代码实现过程

import { promptAction } from '@kit.ArkUI'

@Entry
@Component
struct AlertDialogPage {
  build() {
    RelativeContainer() {
      Button('警告框')
        .id('AlertDialogPageHelloWorld')
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }

        })
        .width('100%')
        .onClick(() => {
          AlertDialog.show({
            title: '温馨提示',
            message: '确定要删除吗?',
            alignment: DialogAlignment.Bottom,
            //点击点击遮障层时,是否关闭弹窗,true表示关闭弹窗。false表示不关闭弹窗。默认值:true
            autoCancel: false,
            primaryButton: {
              value: '确认',
              action: () => {
                promptAction.showToast({
                  message: '点击了确认按钮~~'
                })

              }
            },
            secondaryButton: {
              value: '取消',
              fontColor: '#999999',
              action: () => {
                promptAction.showToast({
                  message: '点击了取消按钮~~'
                })

              }
            }

          })
        })
    }
    .height('100%')
    .width('100%')
    .padding(20)
  }
}

5. 运行效果

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值