0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學(xué)習在線(xiàn)課程
  • 觀(guān)看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區
會(huì )員中心
創(chuàng )作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內不再提示

三種自定義彈窗UI組件封裝的實(shí)現

OpenHarmony技術(shù)社區 ? 來(lái)源:HarmonyOS技術(shù)社區 ? 作者:HarmonyOS技術(shù)社區 ? 2022-03-30 09:28 ? 次閱讀
鴻蒙已經(jīng)提供了全局 UI 方法自定義彈窗,本文是基于基礎的自定義彈窗來(lái)實(shí)現提示消息彈窗、確認彈窗、輸入彈窗的 UI 組件封裝。

消息確認彈窗


首先看下效果:


2c5e3360-af49-11ec-aa7f-dac502259ad0.png

首先先定義一個(gè)新的組件 ConfirmDialog

@CustomDialog
exportdefaultstructConfirmDialog{
title:string=''
content:string=''
confirmFontColor:string='#E84026'
cancelFontColor:string='#0A59F7'
confirmText:string='確認'
cancelText:string='取消'
controller:CustomDialogController
cancel:()=>void
confirm:()=>void
build(){}
}
自定義確認彈窗可自定義傳入的參數有:
  • 可選參數標題 title(默認值:“”),正文內容 content(默認值:“”),確認按鈕字體顏色 confirmFontColor(默認值:#E84026),取消按鈕字體顏色 cancelFontColor(默認值:#0A59F7),確認按鈕文案(默認值:確認),取消按鈕文案(默認值:取消)

  • 必須參數自定義彈窗控制器 controller: CustomDialogController,確認按鈕觸發(fā)事件 confirm(),取消按鈕觸發(fā)事件 cancel()

標題、正文、按鈕封裝:一個(gè)確認彈窗組件主要由標題、正文等文本內容和取消、確認等按鈕事件組成。下面將分別對文案和按鈕通過(guò)**@Extend**裝飾器進(jìn)行封裝。

@Extend 裝飾器將新的屬性函數添加到內置組件上,如 Text、Column、Button 等。通過(guò)**@Extend**裝飾器可以快速定義并復用組件的自定義樣式。

2c77f5d4-af49-11ec-aa7f-dac502259ad0.png

//標題title與正文content自定義樣式
@Extend(Text)functionfancfontSize(fontSize:number){
.fontSize(fontSize)
.width('100%')
.fontColor('rgba(0,0,0,0.86)')
.textAlign(TextAlign.Center)
.padding({top:15,bottom:0,left:8,right:8})
.alignSelf(ItemAlign.Center)
.margin({top:16})
}

//取消、確認按鈕自定義樣式
@Extend(Text)functionfancBtn(fontColor:string){
.fontColor(fontColor)
.backgroundColor(0xffffff)
.width(188)
.height(29)
.fontSize(22)
.textAlign(TextAlign.Center)
}

本示例僅標題與正文僅支持字體大小 fontSize 自定義,按鈕僅支持按鈕文案字體顏色 fontColor 自定義,其他通用屬性皆是寫(xiě)定的,若想支持其他屬性自定義,也可通過(guò) fancfontSize() 添加新的參數。

其次,可以更進(jìn)一步的對標題與正文通過(guò) @Builder 裝飾器進(jìn)行封裝,且通過(guò)是否傳入 title、content 字段來(lái)判斷是否展示對應文案。

@Builder 裝飾的方法用于定義組件的聲明式 UI 描述,在 一個(gè)自定義組件內快速生成多個(gè)布局內容。@Builder 裝飾方法的功能和語(yǔ)法規范與build 函數相同。

//文案樣式
@BuilderTipTextStyle(tip:string,fontSize:number){
Text(tip)
.fancfontSize(fontSize)
.visibility(tip.length>0?Visibility.Visible:Visibility.None)
}

注意:
  • @Extend 裝飾器的內容必須寫(xiě)在 ConfirmDialog{} 組件外,且在 @Extend 裝飾器聲明的基礎內置組件的方法之前不能出現用/*多行注釋?zhuān)〞?huì )報錯),但可采用單行注釋//。

  • @Builder 裝飾器的內容要寫(xiě)在 ConfirmDialog{} 組件內,build(){} 外 。

  • @Builder 裝飾器聲明的自定義組件內部可包含 @Extend 聲明的自定義樣式的基礎組件,但是 @Extend 內部不可包含 @Builder 裝飾器聲明的自定義組件。

ConfirmDialog 組件完整代碼:

//取消、確認按鈕自定義樣式
@Extend(Text)functionfancBtn(fontColor:string){
.fontColor(fontColor)
.backgroundColor(0xffffff)
.width(188)
.height(29)
.fontSize(22)
.textAlign(TextAlign.Center)
}
//標題title與正文content自定義樣式
@Extend(Text)functionfancfontSize(fontSize:number){
.fontSize(fontSize)
.width('100%')
.fontColor('rgba(0,0,0,0.86)')
.textAlign(TextAlign.Center)
.padding({top:15,bottom:0,left:8,right:8})
.alignSelf(ItemAlign.Center)
.margin({top:16})
}
@CustomDialog
exportdefaultstructConfirmDialog{
title:string=''
content:string=''
confirmFontColor:string='#E84026'
cancelFontColor:string='#0A59F7'
confirmText:string='確認'
cancelText:string='取消'
controller:CustomDialogController
cancel:()=>void
confirm:()=>void
//標題、正文文案樣式
@BuilderTipTextStyle(tip:string,fontSize:number){
Text(tip)
.fancfontSize(fontSize)
.visibility(tip.length>0?Visibility.Visible:Visibility.None)
}

build(){
Column(){
this.TipTextStyle(this.title,28)
this.TipTextStyle(this.content,22)
Flex({justifyContent:FlexAlign.SpaceAround}){
Text(this.cancelText)
.fancBtn(this.cancelFontColor)
.onClick(()=>{
this.controller.close()
this.cancel()
})
Text(this.confirmText)
.fancBtn(this.confirmFontColor)
.onClick(()=>{
this.controller.close()
this.confirm()
})
}.margin({top:30,bottom:16,left:16,right:16})
}
}
}

引用頁(yè)面代碼:

importConfirmDialogfrom'./components/dialog/ConfirmDialog.ets'
@Entry
@Component
structIndexComponent{
//確認彈窗
privatetitle:string='標題'
privatecontent:string='此操作將永久刪除該文件,是否繼續?'
privateconfirmText:string='刪除'
ConfirmDialogController:CustomDialogController=newCustomDialogController({
builder:ConfirmDialog({cancel:this.onCancel,confirm:()=>{
this.onAccept()
},title:this.title,content:this.content}),
cancel:this.onCancel,
autoCancel:true
})
//點(diǎn)擊取消按鈕或遮罩層關(guān)閉彈窗
onCancel(){
console.info('取消,關(guān)閉彈窗')
}
//點(diǎn)擊確認彈窗
onAccept(){
console.info('確認,關(guān)閉彈窗')
}
build(){
Scroll(){
Column(){
Text('確認彈窗')
.fontSize(24)
.width(300)
.height(60)
.border({width:5,color:0x317AF7,radius:10,style:BorderStyle.Solid})
.margin({top:20,bottom:10})
.textAlign(TextAlign.Center)
.onClick(()=>{
this.ConfirmDialogController.open()
})
}
.width('100%')
}
}
}

消息提示彈窗

首先看下效果:

2c88cdf0-af49-11ec-aa7f-dac502259ad0.png

首先先定義一個(gè)新的組件 PromptDialog:

@CustomDialog
exportdefaultstructPromptDialog{
controller:CustomDialogController
ancel:()=>void
build(){}
}
至于標題、正文、按鈕文案及按鈕顏色的封裝均與消息確認彈窗一樣,同 1.2 所述。

PromptDialog 組件完整代碼:

//標題title與正文content自定義樣式
@Extend(Text)functionfancfontSize(fontSize:number){
.fontSize(fontSize)
.width('100%')
.fontColor('rgba(0,0,0,0.86)')
.textAlign(TextAlign.Center)
.padding({top:15,bottom:0,left:8,right:8})
.alignSelf(ItemAlign.Center)
.margin({top:16})
}
//底部按鈕自定義樣式
@Extend(Text)functionfancBtn(fontColor:string){
.backgroundColor(0xffffff)
.fontColor(fontColor)
.width(188)
.height(29)
.fontSize(22)
.textAlign(TextAlign.Center)
}
@CustomDialog
exportdefaultstructPromptDialog{
controller:CustomDialogController
cancel:()=>void
//標題、正文文案樣式
@BuilderTipTextStyle(tip:string,fontSize:number){
Text(tip)
.fancfontSize(fontSize)
.visibility(tip.length>0?Visibility.Visible:Visibility.None)
}

build(){
Column(){
this.TipTextStyle($s('strings.title'),28)
this.TipTextStyle($s('strings.content'),22)
Flex({justifyContent:FlexAlign.Center}){
Text($s('strings.confirm'))
.fancBtn(0x0A59F7)
.onClick(()=>{
this.controller.close()
})
}.margin({top:30,bottom:16})
}
}
}

若標題 title 與正文 content 中的文案是固定的,可如此示例一樣,可采用寫(xiě)入到 resource 中的 zh_CN 和 en_US 文件中,通過(guò) $s(‘strings.title’) 取值顯示,若是動(dòng)態(tài)獲取的,可采用消息確認彈窗中傳參方式。

2c993730-af49-11ec-aa7f-dac502259ad0.png

引用頁(yè)面代碼:

importPromptDialogfrom'./components/dialog/PromptDialog.ets'
@Entry
@Component
structIndexComponent{
//消息提示彈窗
PromptDialogController:CustomDialogController=newCustomDialogController({
builder:PromptDialog(),
autoCancel:true
})

build(){
Scroll(){
Column(){
Text('消息提示彈窗')
.fontSize(24)
.width(300)
.height(60)
.border({width:5,color:0x317AF7,radius:10,style:BorderStyle.Solid})
.margin({top:20,bottom:10})
.textAlign(TextAlign.Center)
.onClick(()=>{
this.PromptDialogController.open()
})
}
.width('100%')
}
}
}

消息輸入彈窗

首先看下效果:

2cbbc26e-af49-11ec-aa7f-dac502259ad0.png

首先先定義一個(gè)新的組件 InputDialog:

exportdefaultstructInputDialog{
title:string=''
content:string=''
@StateinputString:string=''
controller:CustomDialogController
cancel:()=>void
confirm:(data)=>void

build(){}
}
此示例講述了子組件通過(guò)事件觸發(fā)傳參給父組件的方法,例如:在子組件用 @state 聲明輸入框內容 inputString,通過(guò) confirm 事件傳參給父組件,可支持在父組件至于標題、正文、按鈕文案及按鈕顏色的封裝均與消息確認彈窗一樣,同 1.2 所述。

PromptDialog 組件完整代碼:

//取消、確認按鈕自定義樣式
@Extend(Text)functionfancBtn(fontColor:string){
.fontColor(fontColor)
.backgroundColor(0xffffff)
.width(188)
.height(29)
.fontSize(22)
.textAlign(TextAlign.Center)
}
//標題title與正文content自定義樣式
@Extend(Text)functionfancfontSize(fontSize:number){
.fontSize(fontSize)
.width('100%')
.fontColor('rgba(0,0,0,0.86)')
.textAlign(TextAlign.Start)
.padding({top:15,bottom:0,left:15,right:15})
.margin({top:16})
}
@CustomDialog
exportdefaultstructInputDialog{
title:string=''
content:string=''
@StateinputString:string=''
controller:CustomDialogController
cancel:()=>void
confirm:(data)=>void
//文案樣式
@BuilderTipTextStyle(tip:string,fontSize:number){
Text(tip)
.fancfontSize(fontSize)
.visibility(tip.length>0?Visibility.Visible:Visibility.None)
}

build(){
Column(){
this.TipTextStyle(this.title,28)
this.TipTextStyle(this.content,22)
//輸入框
TextInput()
.type(InputType.Normal)
.enterKeyType(EnterKeyType.Next)
.caretColor(Color.Green)
.height(44)
.margin({top:20,left:15;right:15})
.alignSelf(ItemAlign.Center)
.onChange((value:string)=>{
this.inputString=value
})
Flex({justifyContent:FlexAlign.SpaceAround}){
Text($s('strings.cancel'))
.fancBtn('#0A59F7')
.onClick(()=>{
this.controller.close()
this.cancel()
})
Text($s('strings.confirm'))
.fancBtn('#E84026')
.onClick(()=>{
this.controller.close()
console.log('inputString:'+this.inputString)
this.confirm(this.inputString)
})
}.margin({top:30,bottom:16,left:16,right:16})
}
}
}
2cd4ba08-af49-11ec-aa7f-dac502259ad0.png

引用頁(yè)面代碼:

importInputDialogfrom'./components/dialog/InputDialog.ets'
@Entry
@Component
structIndexComponent{

//輸入彈窗
privatetext:string='提示'
privatelabel:string='請輸入您的姓名'
InputDialogController:CustomDialogController=newCustomDialogController({
builder:InputDialog({cancel:this.onCancel,confirm:(data)=>{
this.confirm(data)
},title:this.text,content:this.label}),
cancel:this.onCancel,
autoCancel:true
})
//點(diǎn)擊取消按鈕或遮罩層關(guān)閉彈窗
onCancel(){
console.info('取消,關(guān)閉輸入彈窗')
}
//點(diǎn)擊確認彈窗
confirm(data){
console.info('確認,關(guān)閉輸入彈窗,data:'+data)
}

build(){
Scroll(){
Column(){
Text('輸入彈窗')
.fontSize(24)
.width(300)
.height(60)
.border({width:5,color:0x317AF7,radius:10,style:BorderStyle.Solid})
.margin({top:20,bottom:10})
.textAlign(TextAlign.Center)
.onClick(()=>{
this.InputDialogController.open()
})
}
.width('100%')
}
}
}

總結

本文僅僅實(shí)現了三種自定義彈窗 UI 組件的封裝(傳參方式也講解了多種,具體傳參方式可視具體情況而定),待筆者優(yōu)化了功能、代碼后在來(lái)與大家分享, 在最后歡迎鴻蒙各位大佬指教。

原文標題:在鴻蒙上實(shí)現3種自定義彈窗UI組件封裝

文章出處:【微信公眾號:HarmonyOS技術(shù)社區】歡迎添加關(guān)注!文章轉載請注明出處。

審核編輯:湯梓紅
聲明:本文內容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權轉載。文章觀(guān)點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習之用,如有內容侵權或者其他違規問(wèn)題,請聯(lián)系本站處理。 舉報投訴
  • 封裝
    +關(guān)注

    關(guān)注

    124

    文章

    7390

    瀏覽量

    141394
  • 組件
    +關(guān)注

    關(guān)注

    1

    文章

    384

    瀏覽量

    17658
  • 鴻蒙
    +關(guān)注

    關(guān)注

    55

    文章

    1971

    瀏覽量

    42223

原文標題:在鴻蒙上實(shí)現3種自定義彈窗UI組件封裝

文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術(shù)社區】歡迎添加關(guān)注!文章轉載請注明出處。

收藏 人收藏

    評論

    相關(guān)推薦

    OpenHarmony自定義組件

    **可通過(guò)@Builder裝飾器進(jìn)行描述,該裝飾器可以修飾一個(gè)函數,此函數可以在build函數之外聲明,并在build函數中或其他@Builder修飾的函數中使用,從而實(shí)現在一個(gè)自定義組件內快速生成多個(gè)布局內容。**
    的頭像 發(fā)表于 12-08 12:26 ?1536次閱讀

    HarmonyOS開(kāi)發(fā)實(shí)例:【自定義Emitter】

    使用[Emitter]實(shí)現事件的訂閱和發(fā)布,使用[自定義彈窗]設置廣告信息。
    的頭像 發(fā)表于 04-14 11:37 ?721次閱讀
    HarmonyOS開(kāi)發(fā)實(shí)例:【<b class='flag-5'>自定義</b>Emitter】

    HarmonyOS開(kāi)發(fā)案例:【彈窗使用】

    基于dialog和button組件,實(shí)現彈窗的幾種自定義效果
    的頭像 發(fā)表于 04-25 17:44 ?723次閱讀
    HarmonyOS開(kāi)發(fā)案例:【<b class='flag-5'>彈窗</b>使用】

    HarmonyOS開(kāi)發(fā)案例:【 自定義彈窗

    基于A(yíng)rkTS的聲明式開(kāi)發(fā)范式實(shí)現三種不同的彈窗,第一種直接使用公共組件,后兩種使用CustomDialogController實(shí)現
    的頭像 發(fā)表于 05-16 18:18 ?763次閱讀
    HarmonyOS開(kāi)發(fā)案例:【 <b class='flag-5'>自定義</b><b class='flag-5'>彈窗</b>】

    HarmonyOS實(shí)戰開(kāi)發(fā)-全局彈窗封裝案例

    介紹 本示例介紹兩彈窗封裝案例。一自定義彈窗封裝
    發(fā)表于 05-08 15:51

    HarmonyOS實(shí)戰開(kāi)發(fā)-深度探索與打造個(gè)性化自定義組件

    ,容器組件,媒體組件,繪制組件,畫(huà)布組件組件等,如Button、Text 是基礎組件。 由開(kāi)發(fā)者
    發(fā)表于 05-08 16:30

    使用WSC實(shí)自定義COM組件

    今天來(lái)講一下第二方式,第二方式其實(shí)說(shuō)穿了就是在VB6里寫(xiě)好函數封裝成DLL之后生成自定義COM組件。
    發(fā)表于 07-22 06:13

    OpenHarmony應用開(kāi)發(fā)之自定義彈窗

    以??橘子購物??中一個(gè)應用更新提示的彈窗介紹OpenHarmony的自定義彈窗。 接口 自定義彈窗官方文檔:??
    發(fā)表于 09-06 14:40

    OpenHarmony自定義組件介紹

    代碼可復用性、業(yè)務(wù)邏輯與UI分離,后續版本演進(jìn)等因素。因此,將UI和部分業(yè)務(wù)邏輯封裝自定義組件是不可或缺的能力。
    發(fā)表于 09-25 15:36

    鴻蒙上自定義組件的過(guò)程

    特性的組件,通過(guò)擴展 Component 或其子類(lèi)實(shí)現,可以精確控制屏幕元素的外觀(guān),實(shí)現開(kāi)發(fā)者想要達到的效果,也可響應用戶(hù)的點(diǎn)擊、觸摸、長(cháng)按等操作。 ? 下面通過(guò)自定義一個(gè)仿微信朋友圈
    的頭像 發(fā)表于 11-10 09:27 ?2476次閱讀
    鴻蒙上<b class='flag-5'>自定義</b><b class='flag-5'>組件</b>的過(guò)程

    OpenHarmony自定義組件FlowImageLayout

    組件介紹 本示例是OpenHarmony自定義組件FlowImageLayout。 用于將一個(gè)圖片列表以瀑布流的形式顯示出來(lái)。 調用方法
    發(fā)表于 03-21 10:17 ?3次下載
    OpenHarmony<b class='flag-5'>自定義</b><b class='flag-5'>組件</b>FlowImageLayout

    OpenHarmony自定義組件CircleProgress

    組件介紹 本示例是OpenHarmony自定義組件CircleProgress。 用于定義一個(gè)帶文字的圓形進(jìn)度條。 調用方法
    發(fā)表于 03-23 14:06 ?4次下載
    OpenHarmony<b class='flag-5'>自定義</b><b class='flag-5'>組件</b>CircleProgress

    自定義視圖組件教程案例

    自定義組件 1.自定義組件-particles(粒子效果) 2.自定義組件- pulse(脈沖b
    發(fā)表于 04-08 10:48 ?14次下載

    ArkUI如何自定義彈窗(eTS)

    自定義彈窗其實(shí)也是比較簡(jiǎn)單的,通過(guò)CustomDialogController類(lèi)就可以顯示自定義彈窗。
    的頭像 發(fā)表于 08-31 08:24 ?1578次閱讀

    鴻蒙ArkUI實(shí)例:【自定義組件

    組件是 OpenHarmony 頁(yè)面最小顯示單元,一個(gè)頁(yè)面可由多個(gè)組件組合而成,也可只由一個(gè)組件組合而成,這些組件可以是ArkUI開(kāi)發(fā)框架自帶系統
    的頭像 發(fā)表于 04-08 10:17 ?280次閱讀
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看