##HarmonyOS Next快速入门##HarmonyOS应用开发##教育##
CustomDialog是自定义弹窗,可用于广告、中奖、警告、软件更新等与用户交互响应操作。开发者可以通过CustomDialogController类显示自定义弹窗。
接口
constructor(value: CustomDialogControllerOptions)
CustomDialogControllerOptions对象说明
自定义弹窗的界面可以通过装饰器@CustomDialog定义的组件来实现
@CustomDialog
export struct WelcomeDialog {
controller: CustomDialogController
confirm?: () => void
cancel?: () => void
build() {
Column({ space: 10 }) {
Text("欢迎使用")
Text('请同意')
Text('《用户协议》')
Button('同意').onClick(() => {
this.controller.close()
if (this.confirm) {
this.confirm()
}
}).width(150)
Button('不同意')
.width(150)
.backgroundColor(Color.Gray)
.onClick(() => {
this.controller.close()
if (this.cancel) {
this.cancel()
}
})
}.padding(10)
}
}
然后通过CustomDialogController来控制自定义弹窗的显示和隐藏。
import { WelcomeDialog } from './componnents/WelcomeDialog';
@Entry
@Component
struct CustomDialogPage {
@State message: string = '第2节 @CustomDialog自定义弹窗';
@State dialogResult: string = ''
dialogController: CustomDialogController = new CustomDialogController({
builder: WelcomeDialog({
cancel: () => {
this.dialogResult = '不同意'
},
confirm: () => {
this.dialogResult = '同意'
},
}),
})
build() {
Column({space:10}) {
Text(this.message)
.fontSize(20)
.fontWeight(FontWeight.Bold)
Button('自定义弹窗').onClick(() => {
this.dialogController.open()
})
Text('自定义弹窗执行结果:' + this.dialogResult)
}
.height('100%')
.width('100%')
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。