🚀一、CustomDialog
CustomDialog 组件是一种自定义对话框,可以通过开发人员根据特定的要求定制内容和布局。它允许开发人员创建一个完全可定制的对话框,可以显示任何类型的内容,例如文本、图像、表单和按钮。
CustomDialog 通常用于在执行任务之前向用户提供额外的信息或输入选项,例如确认删除操作或输入登录凭据。它们还可以用于创建弹出窗口来显示信息或广告。
CustomDialog 通常涉及创建一个新的布局文件,并扩展 Dialog 类来自定义其行为和外观。
🔎1.创建自定义弹窗
HarmonyOS 的 @CustomDialog 是一个自定义对话框控件,它可以帮助开发人员快速创建各种各样的对话框,包括提示框、确认框、输入框等。
使用 @CustomDialog,开发人员可以自定义对话框的标题、消息、按钮、图标等属性,以及对话框的样式和布局。此外,它还支持自定义对话框的背景、动画和触发事件等属性,以满足不同场景的需求。
@CustomDialogstruct CustomDialogExample { controller: CustomDialogController build() { Column() { Text('我是内容') .fontSize(20) .margin({ top: 10, bottom: 10 }) } }}
@Entry@Componentstruct TextInputSample { dialogController: CustomDialogController = new CustomDialogController({ builder: CustomDialogExample({}), }) build() { Column() { Flex({justifyContent:FlexAlign.Center}){ Button('click me') .onClick(() => { this.dialogController.open() }) }.width('100%') }.padding(20) }}
复制代码
🔎2.弹窗的交互
弹窗数据交互的作用包括提高用户体验、简化流程、提高交互性、实现数据交互和优化 UI 设计等。它可以在用户操作过程中快速展示信息或选项,减少操作繁琐度和时间成本;同时作为数据交互的桥梁,传递用户输入或选择的信息进行处理,展示需要的信息给用户。弹窗数据交互可以提供灵活的交互形式,同时也可以通过多样化的 UI 设计形式实现独特的设计效果。
更多鸿蒙最新技术知识点,请关注作者博客:https://t.doruo.cn/14DjR1rEY
@CustomDialogstruct CustomDialogExample { controller: CustomDialogController cancel: () => void confirm: () => void build() { Column() { Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 }) Flex({ justifyContent: FlexAlign.SpaceAround }) { Button('cancel') .onClick(() => { this.controller.close() this.cancel() }).backgroundColor(0xffffff).fontColor(Color.Black) Button('confirm') .onClick(() => { this.controller.close() this.confirm() }).backgroundColor(0xffffff).fontColor(Color.Red) }.margin({ bottom: 10 }) } }}
@Entry@Componentstruct TextInputSample { dialogController: CustomDialogController = new CustomDialogController({ builder: CustomDialogExample({ cancel: this.onCancel, confirm: this.onAccept, }), alignment: DialogAlignment.Default, // 可设置dialog的对齐方式,设定显示在底部或中间等,默认为底部显示 }) onCancel() { console.info('Callback when the first button is clicked') } onAccept() { console.info('Callback when the second button is clicked') } build() { Column() { Flex({justifyContent:FlexAlign.Center}){ Button('click me') .onClick(() => { this.dialogController.open() }) }.width('100%') }.padding(20) }}
复制代码
🔎3.案例
// xxx.ets@CustomDialogstruct CustomDialogExample { controller: CustomDialogController cancel: () => void confirm: () => void build() { Column() { Text('我是弹窗案例').fontSize(20).margin({ top: 10, bottom: 10 }) Flex({ justifyContent: FlexAlign.SpaceAround }) { Button('取消') .onClick(() => { this.controller.close() this.cancel() }).backgroundColor(0xffffff).fontColor(Color.Black) Button('确认') .onClick(() => { this.controller.close() this.confirm() }).backgroundColor(0xffffff).fontColor(Color.Red) }.margin({ bottom: 10 }) } }}
@Entry@Componentstruct DialogExample { dialogController: CustomDialogController = new CustomDialogController({ builder: CustomDialogExample({ cancel: this.onCancel, confirm: this.onAccept, }), alignment: DialogAlignment.Default, // 可设置dialog的对齐方式,设定显示在底部或中间等,默认为底部显示 }) onCancel() { console.info('取消') } onAccept() { console.info('确认') }
build() { Flex({ justifyContent: FlexAlign.Center }) { Button('点我') .onClick(() => { this.dialogController.open() }) }.width('100%') }}
复制代码
🚀写在最后
评论