介紹
基于ArkTS的聲明式開發(fā)范式實現(xiàn)了三種不同的彈窗,第一種直接使用公共組件,后兩種使用CustomDialogController實現(xiàn)自定義彈窗,效果如圖所示:

相關(guān)概念
- [AlertDialog]:警告彈窗,可設置文本內(nèi)容和響應回調(diào)。
- [CustomDialogController]:通過CustomDialogController類顯示自定義彈窗。
環(huán)境搭建
軟件要求
- [DevEco Studio]版本:DevEco Studio 3.1 Release。
- OpenHarmony SDK版本:API version 9。
硬件要求
- 開發(fā)板類型:[潤和RK3568開發(fā)板]。
- OpenHarmony系統(tǒng):3.2 Release。
環(huán)境搭建
完成本篇Codelab我們首先要完成開發(fā)環(huán)境的搭建,本示例以RK3568開發(fā)板為例,參照以下步驟進行:
- [獲取OpenHarmony系統(tǒng)版本]:標準系統(tǒng)解決方案(二進制)。以3.2 Release版本為例:

- 搭建燒錄環(huán)境。
- [完成DevEco Device Tool的安裝]
- [完成RK3568開發(fā)板的燒錄]
- 搭建開發(fā)環(huán)境。
代碼結(jié)構(gòu)解讀
本篇Codelab只對核心代碼進行講解,對于完整代碼,我們會在gitee中提供。
├──entry/src/main/ets // 代碼區(qū)
│ ├──common
│ │ └──constants
│ │ └──StyleConstants.ets // 抽離樣式
│ │ └──utils
│ │ └──Logger.ets // 日志工具類
│ ├──entryability
│ │ └──EntryAbility.ts // 程序入口類
│ ├──pages
│ │ └──DialogPage.ets // 主界面
│ └──view
│ ├──CustomAlertDialog.ets // 自定義彈窗組件
│ └──ConfirmDialog.ets // 自定義彈窗組件
└──entry/src/main/resources // 資源文件目錄
`HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿`

構(gòu)建頁面
界面主要包括自定義彈窗以及公共組件警告彈窗兩部分,效果如圖所示:

公共彈窗組件
首先創(chuàng)建DialogPage.ets作為主界面,公共彈窗組件直接使用AlertDialog的show方法拉起,效果如圖所示:

// DialogPage.ets
@Entry
@Component
struct DialogPage {
...
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button($r('app.string.one_button_dialog'))
.onClick(() = > {
AlertDialog.show(
{
message: $r('app.string.dialog_message'),
offset: { dx: $r('app.float.dialog_offset_x'), dy: $r('app.float.dialog_offset_y') },
alignment: DialogAlignment.Bottom,
confirm: {
value: $r('app.string.confirm_txt'),
action: () = > {
Logger.info('Button clicking callback');
}
},
cancel: () = > {
Logger.info('Closed callbacks');
}
}
);
})
.height(StyleConstants.BUTTON_HEIGHT)
.width(StyleConstants.BUTTON_WIDTH)
...
}
}
自定義彈窗
通過CustomDialogController的builder屬性設置自定義彈窗組件,調(diào)用open方法拉起彈窗,效果如圖所示:

// DialogPage.ets
@Entry
@Component
struct DialogPage {
dialogControllerExample: CustomDialogController = new CustomDialogController({
builder: ConfirmDialog({ cancel: this.onCancel, confirm: this.onAccept }),
cancel: this.existApp,
autoCancel: true,
alignment: DialogAlignment.Bottom,
customStyle: true,
offset: { dx: $r('app.float.dialog_offset_x'), dy: $r('app.float.dialog_offset_y') }
});
dialogControllerAlert: CustomDialogController = new CustomDialogController({
builder: CustomAlertDialog({ cancel: this.onCancel, confirm: this.onAccept }),
cancel: this.existApp,
autoCancel: true,
alignment: DialogAlignment.Bottom,
customStyle: true,
offset: { dx: $r('app.float.dialog_offset_x'), dy: $r('app.float.dialog_offset_y') }
});
...
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
...
Button($r('app.string.two_button_dialog'))
.onClick(() = > {
this.dialogControllerAlert.open();
})
.margin({ top: $r('app.float.button_margin_top') })
.height(StyleConstants.BUTTON_HEIGHT)
.width(StyleConstants.BUTTON_WIDTH)
Button($r('app.string.customization_dialog'))
.onClick(() = > {
this.dialogControllerExample.open();
})
.margin({ top: $r('app.float.button_margin_top') })
.height(StyleConstants.BUTTON_HEIGHT)
.width(StyleConstants.BUTTON_WIDTH)
}
.width(StyleConstants.FULL_PERCENT)
.height(StyleConstants.FULL_PERCENT)
}
}```**
審核編輯 黃宇
-
鴻蒙
+關(guān)注
關(guān)注
60文章
2839瀏覽量
45334 -
HarmonyOS
+關(guān)注
關(guān)注
80文章
2146瀏覽量
35511 -
OpenHarmony
+關(guān)注
關(guān)注
31文章
3920瀏覽量
20676
發(fā)布評論請先 登錄
HarmonyOS應用自定義鍵盤解決方案
講解一下HarmonyOS中的幾個自定義組件用到的知識
OpenHarmony應用開發(fā)之自定義彈窗
HarmonyOS Codelab 樣例 一彈窗基本使用
鴻蒙上自定義組件的過程
HarmonyOS 中的幾個自定義控件介紹
三種自定義彈窗UI組件封裝的實現(xiàn)
自定義視圖組件教程案例
自定義算子開發(fā)
HarmonyOS開發(fā)案例:【UIAbility和自定義組件生命周期】

HarmonyOS開發(fā)案例:【 自定義彈窗】
評論