HarmonyOS promptAction.openCustomDialog支持选择关闭弹窗方式吗?

HarmonyOS promptAction.openCustomDialog支持选择关闭弹窗方式吗?

HarmonyOS
2024-09-30 16:07:41
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

是的,promptAction.openCustomDialog支持选择关闭弹窗方式。可以通过调用closeCustomDialog方法,并传入对话框id来关闭自定义弹窗。以下是示例代码:

import promptAction from '@ohos.promptAction'  
let customDialogId: number = 0  
@Builder  
function customDialogBuilder() {  
  Column() {  
    Text('Custom dialog Message').fontSize(10)  
    Row() {  
      Button("确认").onClick(() => {  
        promptAction.closeCustomDialog(customDialogId)  
      })  
      Blank().width(50)  
      Button("取消").onClick(() => {  
        promptAction.closeCustomDialog(customDialogId)  
      })  
    }  
  }.height(200).padding(5)  
}  
@Entry  
@Component  
struct Index {  
  @State message: string = 'Hello World'  
  build() {  
    Row() {  
      Column() {  
        Text(this.message)  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)  
          .onClick(() => {  
            promptAction.openCustomDialog({  
              builder: customDialogBuilder.bind(this),  
              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {  
                console.info("reason" + JSON.stringify(dismissDialogAction.reason))  
                console.log("dialog onWillDismiss")  
                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {  
                  dismissDialogAction.dismiss()  
                }  
                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {  
                  dismissDialogAction.dismiss()  
                }  
              }  
            }).then((dialogId: number) => {  
              customDialogId = dialogId  
            })  
          })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
分享
微博
QQ
微信
回复
2024-09-30 17:33:52