鸿蒙开发中@CustomDialog 自定义弹窗内部再弹出自定义弹窗?

阅读 671
avatarAI BotBETA
在鸿蒙开发中,如果你想在 `@CustomDialog` 自定义弹窗内部再弹出自定义弹窗,这是可行的,但需要注意一些细节以避免潜在的问题,如界面重叠、事件冲突等。

你可以通过以下步骤实现:

1. **创建第一个自定义弹窗**:首先,你需要创建并显示一个 `@CustomDialog` 自定义弹窗。

2. **在第一个自定义弹窗内部创建并显示第二个自定义弹窗**:在第一个自定义弹窗的代码中,当满足一定条件时(如按钮点击事件),创建并显示第二个自定义弹窗。确保第二个弹窗的位置和大小不会完全覆盖第一个弹窗的重要部分,以避免用户无法与第一个弹窗交互。

3. **处理事件传递和界面刷新**:确保两个弹窗之间的事件传递(如点击事件)不会相互干扰,并且在关闭第二个弹窗后,第一个弹窗能够正确地刷新和显示。

4. **注意内存和资源管理**:由于同时显示多个弹窗可能会增加内存和资源的消耗,因此要确保在不需要时及时关闭和释放这些弹窗,以避免内存泄漏或性能问题。

通过这种方法,你可以在鸿蒙开发中实现在自定义弹窗内部再弹出自定义弹窗的功能。但请注意,实际开发中可能需要根据具体需求和场景进行调整和优化。
1 个回答

你可以参考下面的示例代码:

@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.existApp, 
    autoCancel: true, 
    alignment: DialogAlignment.Bottom, 
    offset: { dx: 0, dy: -20 }, 
    gridCount: 4, 
    customStyle: false, 
 
  }) 
 
  // 在自定义组件即将析构销毁时将dialogControlle置空 
  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') 
  } 
 
  existApp() { 
    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 }) 
  } 
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进