如何封装一个自定义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 }) 
    } 
  } 
}

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 }) 
  } 
}

参考链接

自定义弹窗(CustomDialog)

分享
微博
QQ
微信
回复
2024-03-18 17:10:59
相关问题
如何在全局实现一个自定义dialog弹窗
275浏览 • 1回复 待解决
如何自定义函数中创建一个UI组件
226浏览 • 1回复 待解决
是否可以自定义权限弹文字
54浏览 • 0回复 待解决
鸿蒙怎么实现自定义布局的Dialog
7258浏览 • 2回复 已解决
如何封装一个通用的commonEvent工具类
188浏览 • 1回复 待解决
ArkTs如何自定义容器组件?
964浏览 • 1回复 待解决
如何自定义Component 属性
12940浏览 • 3回复 待解决
自定义组件中如何添加图片?
762浏览 • 1回复 待解决
js 自定义组件如何传递方法?
4221浏览 • 2回复 待解决
ArkTS如何自定义资源文件
330浏览 • 1回复 待解决
如何设置自定义组件height缺省
145浏览 • 1回复 待解决
如何设置自定义弹窗位置
227浏览 • 1回复 待解决
自定义组件如何导出、引入?
374浏览 • 1回复 待解决
js如何清空一个input 输入的内容
5779浏览 • 1回复 待解决
如何自定义Video组件控制栏样式
388浏览 • 1回复 待解决
如何自定义弹窗中再次弹窗
194浏览 • 1回复 待解决
如何实现H5自定义事件
346浏览 • 1回复 待解决
如何使用和加载自定义字体
160浏览 • 1回复 待解决
如何自定义拼接设置UserAgent参数
371浏览 • 1回复 待解决
鸿蒙中如何自定义字体文件
17181浏览 • 1回复 待解决