HarmonyOS Next 之-组件之弹窗

在ArkUI(方舟UI框架)中,CustomDialog 是一种强大的自定义弹窗工具,适用于广告、中奖提示、警告、软件更新等多种与用户交互的场景。本文将带你了解如何在ArkUI中使用CustomDialog创建自定义弹窗。

一、创建自定义弹窗

使用@CustomDialog装饰器

使用@CustomDialog装饰器来装饰自定义弹窗,并在此装饰器内定义弹窗内容。例如:

typescript 复制代码
@CustomDialog struct MyCustomDialog {

    controller: CustomDialogController = new CustomDialogController({

        builder: MyCustomDialog({})

    });



    build() {

        Column() {

            Text('这是自定义弹窗的内容').fontSize(20).margin({ top: 10, bottom: 10 });

        }

    }

}

创建构造器并绑定onClick事件

创建与装饰器相呼应的构造器,并绑定onClick事件以实现弹窗的弹出。例如:

typescript 复制代码
@Entry @Component struct CustomDialogUser {

    dialogController: CustomDialogController = new CustomDialogController({

        builder: MyCustomDialog()

    });



    build() {

        Column() {

            Button('点击我弹出弹窗').onClick(() => {

                this.dialogController.open();

            }).width('100%').margin({ top: 5 });

        }

    }

}

二、自定义弹窗的数据交互

在自定义弹窗中,你可以添加按钮和数据函数,以实现用户与弹窗之间的数据交互。例如:

typescript 复制代码
@CustomDialog struct TipDialog {

    @State title: string = "提示";

    @State message: string = "";

    @State cancelValue: string = "取消";

    @State confirmValue: string = "确定";

    cancel?: () => void;

    confirm?: () => void;

    controller: CustomDialogController = new CustomDialogController({

        builder: TipDialog()

    });



    build() {

        Column() {

            Text(this.title).width('100%').height(40).textAlign(TextAlign.Center).fontColor(Color.White).backgroundColor($r('app.color.dialog_title_bg'));

            Text(this.message).height(70).width('100%').textAlign(TextAlign.Center);

            Row() {

                Button(this.cancelValue).layoutWeight(1).backgroundColor(Color.White).fontColor(Color.Black).type(ButtonType.Normal).height(40).onClick(() => {

                    this.controller.close();

                    if (this.cancel) {

                        this.cancel();

                    }

                });

                Button(this.confirmValue).layoutWeight(1).type(ButtonType.Normal).backgroundColor($r('app.color.main_color')).fontColor(Color.White).height(40).onClick(() => {

                    this.controller.close();

                    if (this.confirm) {

                        this.confirm();

                    }

                });

            }.width('100%');

        }

    }

}

在页面内,你需要接收这些回调并创建相应的函数操作:

typescript

@Entry @Component struct CustomDialogUser {

    dialogController: CustomDialogController = new CustomDialogController({

        builder: TipDialog({

            title: '警告',

            message: '是否退出?',

            cancel: () => { this.cancelEvent(); },

            confirm: () => { this.confirmEvent(); }

        })

    });



    cancelEvent() {

        console.log('点击了取消');

    }



    confirmEvent() {

        console.log('点击了确定');

    }



    build() {

        Column() {

            Button('显示弹框').onClick(() => {

                this.dialogController.open();

            }).width('100%').margin({ top: 5 });

        }

    }

}

总结:

通过本文,你应该已经掌握了如何在ArkUI中使用CustomDialog创建自定义弹窗,并实现用户与弹窗之间的数据交互。希望这些示例能够帮助你更好地理解和应用ArkUI中的自定义弹窗功能。

相关推荐
xq95271 小时前
HDC Assistant 插件横空出世
harmonyos
OH_TPC9 小时前
【鸿蒙优选三方库】@ohos/aki:一行代码让 ArkTS 调 C++
harmonyos
wenyiyiyiyiyi9 小时前
我用鸿蒙 ArkTS 写了个自己天天用的备忘录:待办、课表和长期目标,全塞进一块桌面卡片
harmonyos
承渊政道12 小时前
鸿蒙设备远控电脑实测:ToDesk蓝牙键鼠、双模式鼠标、跨端剪贴板,远控电脑更顺手了
计算机外设·电脑·harmonyos·远程工作·todesk
CV工程师丁Sir14 小时前
ArkWeb 手记 04|DevTools 调试与缓存清理
java·spring·缓存·harmonyos
程序猿追1 天前
HarmonyOS 6 上做个极简浏览器:Web 组件 + 前进后退 + 加载进度
前端·华为·harmonyos
程序猿追1 天前
HarmonyOS 6 音视频实战:用 AVPlayer 做一个本地音乐播放器
华为·音视频·harmonyos
威哥爱编程1 天前
HarmonyOS 7 多设备 UX 自动检测实战:AppAnalyzer 多设备体检 + 一多工程底线,提前拦截截断重叠大图大字
harmonyos
威哥爱编程1 天前
HarmonyOS 7 安全相机 2.0 实战:数字内容溯源,为拍摄内容提供来源保护
harmonyos
威哥爱编程1 天前
HarmonyOS 7 弱网优化实战:Network Boost Kit 场景加速 + QUIC 长连接 + 弱网直播优化
harmonyos