如何封装一个自定义Dialog对话框

如何封装一个自定义Dialog对话框

HarmonyOS
2024-03-17 14:28:20
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
jmzgh

1.首先自定义Dialog组件:

@CustomDialog 
export default struct CustomDialogExample { 
  @Link textValue: string; 
  @Link inputValue: string; 
  controller: CustomDialogController; 
  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(() => { 
            this.controller.close() 
            this.cancel() 
          }).backgroundColor(0xffffff).fontColor(Color.Black) 
        Button('confirm') 
          .onClick(() => { 
            this.inputValue = this.textValue 
            this.controller.close() 
            this.confirm() 
          }).backgroundColor(0xffffff).fontColor(Color.Red) 
      }.margin({ bottom: 10 }) 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

2.在需要用到Dialog的组件中使用:

import CustomDialogExample from './CustomDialogExample'; 
 
@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.Default, 
    offset: { dx: 0, dy: -20 }, 
    gridCount: 4, 
    customStyle: false 
  }); 
 
  // 在自定义组件即将析构销毁时将dialogController置空 
  aboutToDisappear() { 
    this.dialogController = null 
  } 
 
  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 }) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.

参考链接

自定义弹窗(CustomDialog)

分享
微博
QQ
微信
回复
2024-03-18 17:10:59
相关问题
HarmonyOS 自定义对话框的控制器
595浏览 • 1回复 待解决
如何实现一个自定义询问
1219浏览 • 1回复 待解决
HarmonyOS 如何封装自定义Dialog
847浏览 • 1回复 待解决
如何在全局实现一个自定义dialog弹窗
3695浏览 • 1回复 待解决
HarmonyOS 对话框布局
603浏览 • 1回复 待解决
HarmonyOS 弹出对话框
931浏览 • 1回复 待解决
HarmonyOS 自定义dialog封装后全局调用
824浏览 • 1回复 待解决
HarmonyOS 自定义封装问题
847浏览 • 1回复 待解决
HarmonyOS class中创建对话框不能显示
812浏览 • 1回复 待解决