赞
踩
自定义弹窗需要使用@CustomDialog
装饰器装饰
在其中需要声明一个controller
对象来控制弹窗,对象类型是CustomDialogController
@CustomDialog export struct Buttonfc{ controller: CustomDialogController = new CustomDialogController({ builder: Buttonfc() }) values:string = '' confirm:(value) => void build(){ Column({space:20}){ Text('请输入答案').fontSize(20) TextInput({placeholder:'请输入你的答案'}).type(InputType.Number) .onChange((value)=>{ this.values = value }) Row({space:60}){ Button('确定').onClick((event: ClickEvent) => { this.confirm(this.values) this.controller.close() }) Button('取消').onClick((event: ClickEvent) => { this.controller.close() }) } }.padding(20) } }
controller
对象来控制弹窗controller: CustomDialogController = new CustomDialogController({
builder:Buttonfc({
confirm:(value)=>{
this.answer = value
}}),
alignment:DialogAlignment.Bottom,
offset:{dx:0,dy:-20}
})
其中confirm
是弹窗组件的自定义函数
alignment
可以控制弹窗的位置
offset
控制弹窗相对位置的偏移量
打开弹窗就在父组件中使用controller.open()
this.controller.open()
关闭弹窗需要在弹窗组件中使用controller.close()
this.controller.close()
弹窗组件代码:
@CustomDialog export struct Buttonfc{ controller: CustomDialogController = new CustomDialogController({ builder: Buttonfc() }) values:string = '' confirm:(value) => void build(){ Column({space:20}){ Text('请输入答案').fontSize(20) TextInput({placeholder:'请输入你的答案'}).type(InputType.Number) .onChange((value)=>{ this.values = value }) Row({space:60}){ Button('确定').onClick((event: ClickEvent) => { this.confirm(this.values) this.controller.close() }) Button('取消').onClick((event: ClickEvent) => { this.controller.close() }) } }.padding(20) } }
父组件代码:
import { Buttonfc } from './tc' @Entry @Component struct CustomDialogPage { @State answer: string = '?' Tclist:string[] = ['1','2','3'] indexs:number = 0 controller: CustomDialogController = new CustomDialogController({ builder:Buttonfc({confirm:(value)=>{ this.answer = value }}), alignment:DialogAlignment.Bottom, offset:{dx:0,dy:-20} }) build() { Column({ space: 50 }) { Row() { Text('1+1=') .fontWeight(FontWeight.Bold) .fontSize(30) Text(this.answer) .fontWeight(FontWeight.Bold) .fontSize(30) } Button('作答') .onClick(() => { //弹窗 this.controller.open() }) }.width('100%') .height('100%') .justifyContent(FlexAlign.Center) } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。