HarmonyOS 如何在一个class中创建一个自定义弹框

我的接口写在了一个viewmode的class中,然后这个类中有个接口请求,需要根据返回数据弹一个框,提示用户补录数据,这种场景不知如何实现

HarmonyOS
2024-12-26 14:32:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

可以考虑使用promptaction的opencustomdialog。可封装一层opencustomdialog固定的样式,再通过一个方法直接调用封装好的opencustomdialog。

opencustomdialog实现文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5#promptactionopencustomdialog

以下仅供参考:

Index首页:

用法:

Button("测试").onClick(()=>{
  TestA.getIns().openDialog(()=>{
    hilog.info(0x0000,"ccTest","AAAAA")
  },()=>{
    hilog.info(0x0000,"ccTest","BBBBB")
  })
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

class中:

export class TestA{
  private static Ins:TestA;//单例模式去调用
  constructor() {
    TestA.Ins = this;
  }
  customDialogComponentId: number = 0
  public static getIns(){
    if(!TestA.Ins){
      TestA.Ins = new TestA();
    }
    return TestA.Ins;
  }
  openDialog(sureCB: Function, cancelCB: Function) {
    promptAction.openCustomDialog({
      builder: () => {
        dialogStyleS.getIns().dialogStyle(this.customDialogComponentId ,sureCB, cancelCB);
      }
    }).then((dialogId: number) => {
      this.customDialogComponentId = dialogId
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

弹窗的样式:

@Component
export struct dialogStyleS {
  private static Ins: dialogStyleS;

  constructor() {
    super();
    dialogStyleS.Ins = this;
  }
  public static getIns() {
    if (!dialogStyleS.Ins) {
      dialogStyleS.Ins = new dialogStyleS();
    }
    return dialogStyleS.Ins;
  }
  build() {
  }
  @Builder
  dialogStyle(customDialogComponentId:number,sureCB?: Function, cancelCB?: Function) {
    Column() {
      Text('弹窗').fontSize(30)
      Row({ space: 50 }) {
        Button("确认").onClick(() => {
          sureCB && sureCB()
          promptAction.closeCustomDialog(customDialogComponentId)
        })
        Button("取消").onClick(() => {
          cancelCB && cancelCB()
          promptAction.closeCustomDialog(customDialogComponentId)
        })
      }
    }.height(200).padding(5).justifyContent(FlexAlign.SpaceBetween)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-26 16:56:06
相关问题
如何在自定义函数创建一个UI组件
2543浏览 • 1回复 待解决
如何实现一个自定义询问
1155浏览 • 1回复 待解决
如何封装一个自定义Dialog对话
3008浏览 • 1回复 待解决
使用自定义函数创建一个UI组
928浏览 • 1回复 待解决
HarmonyOS 如何在class启动一个线程
812浏览 • 1回复 待解决
如何在全局实现一个自定义dialog弹窗
3640浏览 • 1回复 待解决
HarmonyOS 实现一个自定义分类列表
1153浏览 • 1回复 待解决
如何快速开发出一个自定义弹窗?
1093浏览 • 1回复 待解决
HarmonyOS一个自定义的tabs冲突
925浏览 • 1回复 待解决