如何在自定义弹窗中再次弹窗

如何在自定义弹窗中再次弹窗


HarmonyOS
2024-03-17 12:49:25
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

1. 通过@CustomDialog创建两个自定义弹窗。

2. 在页面上的Button按钮上设置点击事件,通过调用dialogController.open()方法打开弹窗1。

3. 在弹窗1的Button按钮上设置点击事件,通过dialogControllerTwo.open()方法打开第二个弹窗。

可参考示例代码:

@CustomDialog 
struct CustomDialogExampleTwo { 
  controllerTwo?: CustomDialogController; 
  build() { 
    Column() { 
      Text('我是第二个弹窗') 
        .fontSize(30) 
        .height(100) 
      Button('点我关闭第二个弹窗') 
        .onClick(() => { 
          if (this.controllerTwo != undefined) { 
            this.controllerTwo.close(); 
          } 
        }) 
        .margin(20) 
    } 
  } 
} 
 
@CustomDialog 
struct CustomDialogExample { 
  @Link textValue: string; 
  @Link inputValue: string; 
  dialogControllerTwo: CustomDialogController | null = new CustomDialogController({ 
    builder: CustomDialogExampleTwo(), 
    alignment: DialogAlignment.Bottom, 
    offset: { dx: 0, dy: -25 } }) 
  controller?: CustomDialogController 
  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在所有controller的后面 
  cancel: () => void = () => { 
  } 
  confirm: () => void = () => { 
  } 
  build() { 
    Column() { 
      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 }) 
      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%') 
        .onChange((value: string) => { 
          this.textValue = value; 
        }) 
      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 }) 
      Flex({ justifyContent: FlexAlign.SpaceAround }) { 
        Button('cancel') 
          .onClick(() => { 
            if (this.controller != undefined) { 
              this.controller.close(); 
              this.cancel(); 
            } 
          }).backgroundColor(0xffffff).fontColor(Color.Black) 
        Button('confirm') 
          .onClick(() => { 
            if (this.controller != undefined) { 
              this.inputValue = this.textValue; 
              this.controller.close(); 
              this.confirm(); 
            } 
          }).backgroundColor(0xffffff).fontColor(Color.Red) 
      }.margin({ bottom: 10 }) 
      Button('点我打开第二个弹窗') 
        .onClick(() => { 
          if (this.dialogControllerTwo != null) { 
            this.dialogControllerTwo.open(); 
          } 
        }) 
        .margin(20) 
    }.borderRadius(10) 
    // 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。 
  } 
} 
 
@Entry 
@Component 
struct CustomDialogUser { 
  @State textValue: string = ''; 
  @State inputValue: string = 'click me'; 
  dialogController: CustomDialogController | null = new CustomDialogController({ 
    builder: CustomDialogExample({ 
      cancel: this.onCancel, 
      confirm: this.onAccept, 
      textValue: $textValue, 
      inputValue: $inputValue 
    }), 
    cancel: this.exitApp, 
    autoCancel: true, 
    alignment: DialogAlignment.Bottom, 
    offset: { dx: 0, dy: -20 }, 
    gridCount: 4, 
    customStyle: false, 
    backgroundColor: 0xd9ffffff, 
    cornerRadius: 10, 
  }) 
  // 在自定义组件即将析构销毁时将dialogController置空 
  aboutToDisappear() { 
    this.dialogController = null; // 将dialogController置空 
  } 
  onCancel() { 
    console.info('Callback when the first button is clicked'); 
  } 
  onAccept() { 
    console.info('Callback when the second button is clicked'); 
  } 
  exitApp() { 
    console.info('Click the callback in the blank area'); 
  } 
  build() { 
    Column() { 
      Button(this.inputValue) 
        .onClick(() => { 
          if (this.dialogController != null) { 
            this.dialogController.open(); 
          } 
        }).backgroundColor(0x317aff) 
    }.width('100%').margin({ top: 5 }) 
  } 
}

详细内容可以参考:自定义弹窗

分享
微博
QQ
微信
回复
2024-03-17 22:24:37
相关问题
自定义弹窗的变量如何传递给页面
377浏览 • 1回复 待解决
如何设置自定义弹窗位置
229浏览 • 1回复 待解决
如何理解自定义弹窗的gridCount参数
361浏览 • 1回复 待解决
如何在全局实现一个自定义dialog弹窗
279浏览 • 1回复 待解决
如何去除自定义弹窗的白色背景
296浏览 • 1回复 待解决
自定义弹窗大小如何自适应内容
339浏览 • 1回复 待解决
弹窗打开、关闭动画是否支持自定义
320浏览 • 1回复 待解决
如何在js文件引入自定义js文件
5744浏览 • 1回复 待解决
如何在自定义函数创建一个UI组件
226浏览 • 1回复 待解决
自定义组件如何添加图片?
762浏览 • 1回复 待解决
鸿蒙如何自定义字体文件
17181浏览 • 1回复 待解决
Web如何创建定位授权弹窗
182浏览 • 1回复 待解决
如何自定义Component 属性
12940浏览 • 3回复 待解决
ArkTs如何自定义容器组件?
964浏览 • 1回复 待解决
自定义组件如何导出、引入?
374浏览 • 1回复 待解决
如何设置自定义组件height缺省
145浏览 • 1回复 待解决
js 自定义组件如何传递方法?
4226浏览 • 2回复 待解决
ArkTS如何自定义资源文件
333浏览 • 1回复 待解决