鴻蒙(HarmonyOS)常見的三種彈窗方式
最近有一個想法,做一個針對鴻蒙官方API的工具箱項目,介紹常用的控件,以及在項目中如何使用,今天介紹Harmony中如何實現(xiàn)彈窗功能。
警告彈窗
警告彈窗是一個App中非常常用的彈窗,例如:
- 刪除一條記錄,提示一下用戶:您確定要刪除嗎?
- 在App首頁,點擊返回時,提示一下用戶:您確定要退出App嗎?
使用AlertDialog.show方法進行彈窗,這個方法支持傳入以下三個類中的任意一個對象
- AlertDialogParamWithConfirm
- AlertDialogParamWithButtons
- AlertDialogParamWithOptions
以AlertDialogParamWithButtons對象進行說明,下面表格介紹常用屬性:
| 參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
|---|---|---|---|
| title | ResourceStr | 否 | 彈窗標(biāo)題 |
| message | ResourceStr | 是 | 彈窗內(nèi)容 |
| autoCancel | boolean | 否 | 點擊遮障層時,是否關(guān)閉彈窗。默認(rèn)值:true |
| primaryButton | 否 | 按鈕的文本內(nèi)容、文本色、按鈕背景色和點擊回調(diào) | |
| secondaryButton | 否 | 按鈕的文本內(nèi)容、文本色、按鈕背景色和點擊回調(diào) | |
| cancel | () => void | 否 | 點擊遮障層關(guān)閉dialog時的回調(diào) |
| alignment | DialogAlignment | 否 | 彈窗在豎直方向上的對齊方式。默認(rèn)值:DialogAlignment.Default |
接下來,我們用代碼來實現(xiàn)一下:
AlertDialog.show({
title:"彈窗標(biāo)題",
message:"這是彈窗內(nèi)容",
autoCancel:true,//點擊遮障層時,是否關(guān)閉彈窗。默認(rèn)值:true
alignment: DialogAlignment.Center,//彈窗在豎直方向上的對齊方式。默認(rèn)值:DialogAlignment.Default
primaryButton: {
value: "取消",
fontColor: '#181818',
action: () => {
AppUtil.showToast("點擊了取消按鈕");
}
},
secondaryButton: {
value: "確定",
fontColor: $r('app.color.mainColor'),
action: () => {
AppUtil.showToast("點擊了確定按鈕");
}
},
cornerRadius:12,//彈窗邊框弧度
width:'80%', //彈窗寬度
cancel:()=>{
AppUtil.showToast("點擊遮障層關(guān)閉dialog時的回調(diào)");
}
})
效果圖:

參考官方鏈接:
自定義彈窗
自定義彈窗相比警告彈窗更為靈活,支持自定義彈窗的樣式與內(nèi)容。
自定義彈窗的參數(shù):
| 參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
|---|---|---|---|
| builder | CustomDialog | 是 | 自定義彈窗內(nèi)容構(gòu)造器。 |
| cancel | () => void | 否 | 點擊遮障層退出時的回調(diào)。 |
| autoCancel | boolean | 否 | 是否允許點擊遮障層退出。默認(rèn)值:true |
| alignment | DialogAlignment | 否 | 彈窗在豎直方向上的對齊方式。默認(rèn)值:DialogAlignment.Default |
| offset | Offset | 否 | 彈窗相對alignment所在位置的偏移量。 |
| customStyle | boolean | 否 | 彈窗容器樣式是否自定義。默認(rèn)值:false,彈窗容器的寬度根據(jù)柵格系統(tǒng)自適應(yīng),不跟隨子節(jié)點;高度自適應(yīng)子節(jié)點,最大為窗口高度的90%;圓角為24vp。 |
| gridCount8+ | number | 否 | 彈窗寬度占柵格寬度的個數(shù)。默認(rèn)為按照窗口大小自適應(yīng),異常值按默認(rèn)值處理,最大柵格數(shù)為系統(tǒng)最大柵格數(shù)。 |
代碼實現(xiàn)
我們使用自定義彈窗實現(xiàn)隱私政策彈窗,新建PrivacyPolicyDialogBackUp類,也就是內(nèi)容構(gòu)造器,使用@CustomDialog修飾,內(nèi)部有一個屬性controller: CustomDialogController,這些都是常規(guī)寫法,然后在build中實現(xiàn)界面布局。
@CustomDialog
export default struct PrivacyPolicyDialogBackUp{
controller: CustomDialogController
cancel!: () => void
confirm!: () => void
build() {
Column() {
Text($r('app.string.simple_user_policy')).fontSize(18).fontColor($r('app.color.title_color')).margin({ top: 30, bottom: 19 })
Scroll(){
Text(){
Span($r('app.string.privacy_policy_start'))
Span($r('app.string.user_agreement_two')).fontColor($r('app.color.mainColor')).onClick(() => {
this.openWebUrl("/useragreement.html");
})
Span($r('app.string.and'))
Span($r('app.string.privacy_policy_two')).fontColor($r('app.color.mainColor')).onClick(() => {
this.openWebUrl("/privacypolicy.html");
})
Span($r('app.string.simple_privacy_policy'))
}.fontSize(16).fontColor($r('app.color.body_color')).margin({
left:25,
right:25
})
}.height(120)
Column(){
Button($r('app.string.disagree_privacy_policy')).onClick(() => {
this.controller.close();
this.cancel();
}).fontColor($r('app.color.other_color')).fontSize(15).backgroundColor(Color.Transparent)
Button($r('app.string.agree_privacy_policy')).onClick(() => {
this.controller.close();
this.confirm();
}).fontColor($r('app.color.white')).fontSize(17)
.linearGradient({
direction: GradientDirection.Right, colors:[[$r('app.color.start_main_color'),0.0],[$r('app.color.end_main_color'),1.0]]
}).width('80%').margin({
top:15,bottom:21
}).borderRadius(24)
}
}
}
openWebUrl(urlSuffix:string){
let url= AppConstant.URL+urlSuffix;
logger.info("url:"+url)
router.pushUrl({
url: Pages.WebViewPage,
params:{
data1: 'message',
url: url, // 傳遞的 URL 參數(shù)
}
}, router.RouterMode.Single)
}
}
在組件中創(chuàng)建CustomDialogController實例,指定builder屬性,就是上面寫的內(nèi)容構(gòu)造器
privacyPolicyDialog: CustomDialogController = new CustomDialogController({
builder: PrivacyPolicyDialog({
cancel:this.onCancel.bind(this),
confirm:this.onAgree.bind(this)
}),
alignment: DialogAlignment.Default, // 可設(shè)置dialog的對齊方式,設(shè)定顯示在底部或中間等,默認(rèn)為底部顯示
cornerRadius:13,
autoCancel:false
})
顯示彈窗
this.privacyPolicyDialog.open();
關(guān)閉彈窗
this.privacyPolicyDialog.close();
效果圖:

參考官方鏈接:
加載中彈窗
加載中彈窗彈窗其實就是自定義彈窗實現(xiàn),只是內(nèi)容構(gòu)造器不一樣而已,給Image組件設(shè)置animation動畫,無限循環(huán)圖片
@CustomDialog
export default struct LoadingDialog {
controller: CustomDialogController
private loadingText: string|Resource = "加載中..."
@State angle:number = 10
aboutToAppear(){
setTimeout(()=>{
this.angle = 1440 // 設(shè)定一個大的旋轉(zhuǎn)角度,確保動畫執(zhí)行
},100)
}
build() {
Column(){
Image($r('app.media.icon_loading_3'))
.width(70)
.height(70)
.rotate({angle:this.angle})
.animation({
duration: 5000,
curve: Curve.Linear,
delay: 0,
iterations: -1, // 設(shè)置-1表示動畫無限循環(huán)
playMode: PlayMode.Normal
})
Text(this.loadingText).fontSize(14).fontColor(0xffffff).margin({top:10})
}.backgroundColor(0x88000000).borderRadius(10).padding({
left:20,right:20,top:10,bottom:10
})
}
}
效果圖:

源碼下載:
如果您想第一時間看我的后期文章,掃碼關(guān)注公眾號
安輝編程筆記 - 開發(fā)技術(shù)分享
掃描二維碼加關(guān)注


浙公網(wǎng)安備 33010602011771號