那曲檬骨新材料有限公司

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

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

3天內(nèi)不再提示

在OpenHarmony上如何使用不同的彈窗

OpenHarmony技術(shù)社區(qū) ? 來源:OST開源開發(fā)者 ? 2023-06-18 15:10 ? 次閱讀

應(yīng)用中經(jīng)常用到彈窗,比如警告彈窗、日期選擇彈窗、文本選擇彈窗以及其他自定義彈窗等等。本例將為大家介紹如何使用不同的彈窗。

效果呈現(xiàn)

本例最終效果如下:

83f5bbfe-0da6-11ee-962d-dac502259ad0.gif

示例中共涉及四類彈窗:

警告彈窗:提示信息尚未保存。

日期滑動(dòng)選擇器彈窗:選擇出生日期。

文本滑動(dòng)選擇器彈窗:選擇性別。

自定義彈窗:填寫興趣愛好。

說明:自定義彈窗可以根據(jù)業(yè)務(wù)需要自行定義彈窗的形式和內(nèi)容,比如文本輸入、單選、多選等等,本例以文本輸入為例進(jìn)行介紹。

運(yùn)行環(huán)境

本例基于以下環(huán)境開發(fā),開發(fā)者也可以基于其他適配的版本進(jìn)行開發(fā):

IDE:DevEco Studio 3.1 Release

SDK:Ohos_sdk_public 3.2.12.5(API Version 9 Release)

實(shí)現(xiàn)思路

本例中涉及的 4 類彈窗及實(shí)現(xiàn)方案如下:

警告彈窗:使用 AlertDialog 實(shí)現(xiàn)。

日期滑動(dòng)選擇器彈窗:使用 DatePickerDialog 實(shí)現(xiàn)。

文本滑動(dòng)選擇器彈窗:使用 TextPickerDialog 實(shí)現(xiàn)。

自定義彈窗:使用 CustomDialogController 實(shí)現(xiàn)。

開發(fā)步驟

由于本例重點(diǎn)講解對(duì)話框的使用,所以開發(fā)步驟會(huì)著重講解相關(guān)實(shí)現(xiàn),不相關(guān)的內(nèi)容不做介紹,全量代碼可參考完整代碼章節(jié)。

①首先,使用 AlertDialog 實(shí)現(xiàn)警告彈窗

通過 message 參數(shù)設(shè)置告警信息,alignment 設(shè)置彈窗在界面中垂直方向的對(duì)齊方式;通過 primaryButton 和 secondaryButton 添加按鈕。

具體代碼如下:

alertDialog(context:Context.UIAbilityContext){
AlertDialog.show({
//通過message設(shè)置告警信息
message:'當(dāng)前數(shù)據(jù)未保存,是否確認(rèn)離開?',
//通過alignment設(shè)置彈窗在界面垂直方向的對(duì)齊方式,此處設(shè)置為底部對(duì)齊
alignment:DialogAlignment.Bottom,
//通過offset設(shè)置基于對(duì)齊位置的便宜量
offset:{
dx:0,
dy:-20
},
//彈窗中左起第一個(gè)按鈕
primaryButton:{
value:'取消',
action:()=>{
console.info('Callbackcancelbuttonisclicked');
}
},
//彈窗中左起第二個(gè)按鈕
secondaryButton:{
value:'確定',
action:()=>{
//Exitingtheapp.
context.terminateSelf();
console.info('Callbackdefinitebuttonisclicked');
}
}
});
}
②使用 DatePickerDialog 實(shí)現(xiàn)日期滑動(dòng)選擇器彈窗

通過 start 和 end 分別設(shè)置日期區(qū)間的起始時(shí)間和末尾時(shí)間;通過 lunar 設(shè)置使用農(nóng)歷還是陽歷;使用 onAccept 監(jiān)聽選擇的日期,本例中通過變量 selectedDate 將選中的日期設(shè)置給參數(shù) selected,這樣彈窗彈出時(shí)的日期就默認(rèn)為上次選中的日期。

具體代碼如下:

datePickerDialog(dateCallback){
DatePickerDialog.show({
start:newDate('1900-1-1'),
end:newDate('2100-1-1'),
//通過變量selectedDate將選中的日期設(shè)置給參數(shù)selected
selected:this.selectedDate,
lunar:false,
//使用onAccept監(jiān)聽選擇的日期
onAccept:(value:DatePickerResult)=>{
letyear=value.year;
letmonth=value.month+1;
letday=value.day;
letbirthdate:string=this.getBirthDateValue(year,month,day);
//通過setFullYear將選中的日期傳遞給變量selectedDate
this.selectedDate.setFullYear(value.year,value.month,value.day)
//返回選中的日期
dateCallback(birthdate);
}
});
}
③使用 TextPickerDialog 實(shí)現(xiàn)文本滑動(dòng)選擇器彈窗

通過 range 設(shè)置文本選擇項(xiàng),使用 onAccept 監(jiān)聽選擇的文本項(xiàng),本例中通過變量 selectedGender 將選中的性別的索引設(shè)置給參數(shù) selected,這樣彈窗彈出時(shí)的性別就默認(rèn)為上次選中的性別。

具體代碼如下:

textPickerDialog(sexArray:Resource,sexCallback){
//判斷文本項(xiàng)的列表是否為空
if(this.isEmptyArr(sexArray)){
console.error('sexisnull');
return;
}
TextPickerDialog.show({
//通過range設(shè)置文本選擇項(xiàng)
range:sexArray,
//通過變量selectedGender將選中的性別的索引設(shè)置給參數(shù)selected
selected:this.selectedGender,
//使用onAccept監(jiān)聽選擇的文本項(xiàng)
onAccept:(result:TextPickerResult)=>{
sexCallback(result.value);
//獲取選中項(xiàng)的索引
this.selectedGender=result.index
},
onCancel:()=>{
console.info('TextPickerDialogonCancel');
}
});
}

④使用 CustomDialogController 實(shí)現(xiàn)自定義彈窗

當(dāng)現(xiàn)有彈窗不能滿足業(yè)務(wù)訴求時(shí),開發(fā)者可以自行設(shè)計(jì)彈窗的樣式。在實(shí)現(xiàn)自定義彈窗時(shí),需要將彈窗的 UI 放在被 @CustomDialog 修飾的自定義組件中,然后使用 CustomDialogController 的實(shí)例來控制彈窗的彈出和關(guān)閉。

具體代碼如下:

//使用@CustomDialog修飾自定義彈窗
@CustomDialog
structCustomDialogFrame{
...
//定義CustomDialogController
controller:CustomDialogController

build(){
Column(){
Text('興趣愛好').fontSize(20).margin({top:10,bottom:10})
TextInput({placeholder:'',text:this.textValue}).height(60).width('90%')
.onChange((value:string)=>{
this.textValue=value
})
Flex({justifyContent:FlexAlign.SpaceAround}){
Button('取消')
.onClick(()=>{
//點(diǎn)擊‘取消’,彈窗關(guān)閉
this.controller.close()
})
.backgroundColor('')
.fontColor('#007DFF')
Button('保存')
.onClick(()=>{
this.inputValue=this.textValue
//點(diǎn)擊‘保存’,彈窗關(guān)閉
this.controller.close()
})
.backgroundColor(0xffffff)
.fontColor('#007DFF')
}.margin({bottom:10})
}.justifyContent(FlexAlign.Start)
}
}
...
//實(shí)例化自定義彈窗
customDialogController:CustomDialogController=newCustomDialogController({
//使用上文創(chuàng)建的自定義彈窗進(jìn)行實(shí)例化
builder:CustomDialogFrame({
textValue:$textValue,
inputValue:$inputValue
}),
alignment:DialogAlignment.Bottom,
offset:{
dx:0,
dy:-20
}
});
...


完整代碼

本例完整代碼如下:

importContextfrom'@ohos.app.ability.common';
importhilogfrom'@ohos.hilog';

@Component
structTextFrame{
@Linkcontent:string;
privatetextImage:Resource;
privatetext:string;
onTextClick:()=>void;

build(){
Row(){
Image(this.textImage)
.width(24)
.height(24)
.margin({left:12})
Text(this.text)
.fontSize(16)
.margin({left:12})
.height(24)
Text(this.content)
.fontSize(16)
.textAlign(TextAlign.End)
.textOverflow({overflow:TextOverflow.Ellipsis})
.maxLines(1)
.margin({
left:16,
right:7
})
.layoutWeight(1)
.width('100%')
Image($r('app.media.ic_arrow'))
.width(12)
.height(24)
.margin({right:14})
}
.margin({top:24})
.borderRadius(24)
.backgroundColor(Color.White)
.width('93.3%')
.height(64)
.onClick(this.onTextClick)
}
}

@Component
structInputFrame{
privateinputImage:Resource;
privatehintText:string;

build(){
Row(){
Image(this.inputImage)
.width(24)
.height(24)
.margin({left:12})
TextInput({placeholder:this.hintText})
.fontSize(16)
.padding({left:12})
.placeholderColor('#99000000')
.backgroundColor(Color.White)
.fontWeight(FontWeight.Normal)
.fontStyle(FontStyle.Normal)
.fontColor(Color.Black)
.margin({right:32})
.layoutWeight(1)
.height(48)
}
.margin({top:24})
.borderRadius(24)
.backgroundColor(Color.White)
.width('93.3%')
.height(64)
}
}

@CustomDialog
structCustomDialogFrame{
@LinktextValue:string
@LinkinputValue:string
controller:CustomDialogController

build(){
Column(){
Text('興趣愛好').fontSize(20).margin({top:10,bottom:10})
TextInput({placeholder:'',text:this.textValue}).height(60).width('90%')
.onChange((value:string)=>{
this.textValue=value
})
Flex({justifyContent:FlexAlign.SpaceAround}){
Button('取消')
.onClick(()=>{
this.controller.close()
}).backgroundColor('').fontColor('#007DFF')
Button('保存')
.onClick(()=>{
this.inputValue=this.textValue
this.controller.close()
}).backgroundColor(0xffffff).fontColor('#007DFF')
}.margin({bottom:10})
}.justifyContent(FlexAlign.Start)
}
}

@Entry
@Component
structIndex{
@Statebirthdate:string='';
@Statesex:string='';
@StatetextValue:string='';
@StateinputValue:string='';
selectedDate:Date=newDate("2010-1-1")
selectedGender:number=0
privatesexArray:Resource=$r('app.strarray.sex_array');
customDialogController:CustomDialogController=newCustomDialogController({
builder:CustomDialogFrame({
textValue:$textValue,
inputValue:$inputValue
}),
alignment:DialogAlignment.Bottom,
offset:{
dx:0,
dy:-20
}
});

alertDialog(context:Context.UIAbilityContext){
AlertDialog.show({
message:'當(dāng)前數(shù)據(jù)未保存,是否確認(rèn)離開?',
alignment:DialogAlignment.Bottom,
offset:{
dx:0,
dy:-20
},
primaryButton:{
value:'取消',
action:()=>{
console.info('Callbackcancelbuttonisclicked');
}
},
secondaryButton:{
value:'確定',
action:()=>{
//Exitingtheapp.
context.terminateSelf();
console.info('Callbackdefinitebuttonisclicked');
}
}
});
}

datePickerDialog(dateCallback){
DatePickerDialog.show({
start:newDate('1900-1-1'),
end:newDate('2100-1-1'),
selected:this.selectedDate,
lunar:false,
onAccept:(value:DatePickerResult)=>{
letyear=value.year;
letmonth=value.month+1;
letday=value.day;
letbirthdate:string=this.getBirthDateValue(year,month,day);
this.selectedDate.setFullYear(value.year,value.month,value.day)
dateCallback(birthdate);
}
});
}

textPickerDialog(sexArray:Resource,sexCallback){
if(this.isEmptyArr(sexArray)){
console.error('sexisnull');
return;
}
TextPickerDialog.show({
range:sexArray,
selected:this.selectedGender,
onAccept:(result:TextPickerResult)=>{
sexCallback(result.value);
this.selectedGender=result.index
},
onCancel:()=>{
console.info('TextPickerDialogonCancel');
}
});
}

getBirthDateValue(year:number,month:number,day:number):string{
letbirthdate:string=`${year}${'年'}${month}`+
`${'月'}${day}${'日'}`;
returnbirthdate;
}

isEmpty(obj):boolean{
returnobj===undefined||obj===null||obj==='';
}

isEmptyArr(array):boolean{
returnthis.isEmpty(array)||array.length===0;
}

build(){
Row(){
Column(){
Row(){
Image($r('app.media.ic_back'))
.width(26)
.height(26)
.alignSelf(ItemAlign.Start)
.margin({
left:'7.2%',
top:19
})
.onClick(()=>{
letcontext=getContext(this)asContext.UIAbilityContext;
this.alertDialog(context);
})
Text('個(gè)人信息')
.fontColor(Color.Black)
.fontSize(20)
.margin({top:20,left:20})
.alignSelf(ItemAlign.Center)
}.width('100%')
Image($r('app.media.ic_avatar'))
.width(56)
.height(56)
.alignSelf(ItemAlign.Center)
.margin({top:'5.5%'})
Text('頭像')
.fontColor(Color.Black)
.fontSize(16)
.margin({top:'2.1%'})
.alignSelf(ItemAlign.Center)
InputFrame({
inputImage:$r('app.media.ic_nickname'),
hintText:'昵稱'
})
TextFrame({
textImage:$r('app.media.ic_birthdate'),
text:'出生日期',
content:$birthdate,
onTextClick:()=>{
this.datePickerDialog((birthValue:string)=>{
this.birthdate=birthValue;
});
}
})
TextFrame({
textImage:$r('app.media.ic_sex'),
text:'性別',
content:$sex,
onTextClick:()=>{
this.textPickerDialog(this.sexArray,(sexValue:string)=>{
this.sex=sexValue;
});
}
})
InputFrame({
inputImage:$r('app.media.ic_signature'),
hintText:'個(gè)性簽名'
})
TextFrame({
textImage:$r('app.media.ic_hobbies'),
text:'興趣愛好',
content:$textValue,
onTextClick:()=>{
this.customDialogController.open();
}
})
}
.backgroundColor('#F5F5F5')
.height('100%')
.width('100%')
}
.height('100%')
}
}



審核編輯:劉清

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • OpenHarmony
    +關(guān)注

    關(guān)注

    25

    文章

    3744

    瀏覽量

    16577

原文標(biāo)題:OpenHarmony上使用彈窗

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

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    鴻蒙原生開源庫ViewPoolOpenHarmony社區(qū)正式上線

    近日,由伙伴參與共建的鴻蒙原生開源庫“ViewPool”OpenHarmony社區(qū)正式上線。這個(gè)開發(fā)庫是基于OpenHarmony技術(shù)孵化的成果,充分發(fā)揮了平臺(tái)的技術(shù)特性,同時(shí)融入了伙伴
    的頭像 發(fā)表于 12-20 14:44 ?304次閱讀

    OpenHarmony人才生態(tài)大會(huì)南向生態(tài)社區(qū)發(fā)展論壇武漢圓滿舉辦

    11月27日,OpenHarmony人才生態(tài)大會(huì)2024武漢隆重舉行。當(dāng)日下午的 OpenHarmony南向生態(tài)社區(qū)發(fā)展論壇(以下簡稱“論壇”),眾多社區(qū)伙伴、企業(yè)代表、技術(shù)專家與
    的頭像 發(fā)表于 11-29 10:06 ?237次閱讀
    <b class='flag-5'>OpenHarmony</b>人才生態(tài)大會(huì)南向生態(tài)社區(qū)發(fā)展論壇<b class='flag-5'>在</b>武漢圓滿舉辦

    OpenHarmony人才生態(tài)大會(huì)南向生態(tài)社區(qū)發(fā)展論壇武漢圓滿舉辦

    11月27日,OpenHarmony人才生態(tài)大會(huì)2024武漢隆重舉行。當(dāng)日下午的 OpenHarmony南向生態(tài)社區(qū)發(fā)展論壇(以下簡稱“論壇”),眾多社區(qū)伙伴、企業(yè)代表、技術(shù)專家與
    發(fā)表于 11-29 09:54

    基于ArkTS語言的OpenHarmony APP應(yīng)用開發(fā):HelloOpenharmony

    1、程序簡介該程序是基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)編寫的UI應(yīng)用類:HelloOpenHarmony。本案例是基于API9接口開發(fā)。本案例已在OpenHarmony凌蒙派-RK3568開發(fā)
    的頭像 發(fā)表于 09-15 08:09 ?480次閱讀
    基于ArkTS語言的<b class='flag-5'>OpenHarmony</b> APP應(yīng)用開發(fā):Hello<b class='flag-5'>Openharmony</b>

    第二屆大會(huì)回顧第25期 | OpenHarmony的Python設(shè)備應(yīng)用開發(fā)

    Python以其簡單、易學(xué)和功能強(qiáng)大而聞名,有著廣泛的用戶群體。采用Python開發(fā)有助于降低OpenHarmony的學(xué)習(xí)門檻。如何在OpenHarmony用Python開發(fā)設(shè)備應(yīng)用,有哪些關(guān)鍵技術(shù)?電
    的頭像 發(fā)表于 08-27 11:53 ?783次閱讀
    第二屆大會(huì)回顧第25期 | <b class='flag-5'>OpenHarmony</b><b class='flag-5'>上</b>的Python設(shè)備應(yīng)用開發(fā)

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

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

    如何在OpenHarmony設(shè)置靜態(tài)IP?

    介紹本文適用于所有RK3566/RK3568/RK3588平臺(tái)產(chǎn)品OpenHarmony系統(tǒng)設(shè)置靜態(tài)IP。本文以PurplePiOH開發(fā)板為例,
    的頭像 發(fā)表于 05-12 08:32 ?826次閱讀
    如何在<b class='flag-5'>OpenHarmony</b>設(shè)置靜態(tài)IP?

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

    介紹 本示例介紹兩種彈窗的封裝案例。一種是自定義彈窗封裝成自定義組件的方式,使用一句代碼即可控制顯示;一種是使用子窗口的方式實(shí)現(xiàn)彈窗,使用一句代碼即可展示。 效果預(yù)覽圖 使用說明 進(jìn)入首頁會(huì)立馬
    發(fā)表于 05-08 15:51

    HarmonyOS開發(fā)案例:【多種樣式彈窗

    如何使用彈窗功能,實(shí)現(xiàn)四種類型彈窗。分別是:警告彈窗、自定義彈窗、日期滑動(dòng)選擇器彈窗、文本滑動(dòng)選擇器彈窗
    的頭像 發(fā)表于 05-08 15:32 ?853次閱讀
    HarmonyOS開發(fā)案例:【多種樣式<b class='flag-5'>彈窗</b>】

    HarmonyOS實(shí)戰(zhàn)開發(fā)-如何使用全局狀態(tài)保留能力彈窗來實(shí)現(xiàn)評(píng)論組件。

    介紹 評(píng)論組件目前市面上的短視頻app中是一種很常見的場景,本案例使用全局狀態(tài)保留能力彈窗來實(shí)現(xiàn)評(píng)論組件。點(diǎn)擊評(píng)論按鈕彈出評(píng)論組件,點(diǎn)擊空白處隱藏該組件,再次點(diǎn)擊評(píng)論按鈕則會(huì)恢復(fù)一次瀏覽的組件
    發(fā)表于 05-07 15:06

    HarmonyOS實(shí)戰(zhàn)開發(fā)-全局狀態(tài)保留能力彈窗

    ,使用GlobalStateDialogManager.getGlobalStateDialogNodeController().setUIContext(this.getUIContext())。 全局入口頁設(shè)置彈窗位置
    發(fā)表于 05-07 14:53

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

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

    鴻蒙OpenHarmony【創(chuàng)建工程并獲取源碼】

    通過DevEco Device Tool創(chuàng)建OpenHarmony工程時(shí),可自動(dòng)下載相應(yīng)版本的OpenHarmony源碼。
    的頭像 發(fā)表于 04-19 21:40 ?420次閱讀
    鴻蒙<b class='flag-5'>OpenHarmony</b>【創(chuàng)建工程并獲取源碼】

    OpenHarmony內(nèi)核編程實(shí)戰(zhàn)

    編程入門[Hello,OpenHarmony]正式開始之前,對(duì)于剛接觸OpenHarmony的伙伴們,面對(duì)大篇幅的源碼可能無從下手,不知道怎么去編碼寫程序,下面用一個(gè)簡單的例子帶伙伴們?nèi)腴T。▍任務(wù)
    的頭像 發(fā)表于 03-27 08:31 ?942次閱讀
    <b class='flag-5'>OpenHarmony</b>內(nèi)核編程實(shí)戰(zhàn)

    介紹一種OpenAtom OpenHarmony輕量系統(tǒng)適配方案

    本文不改變?cè)邢到y(tǒng)基礎(chǔ)框架的基礎(chǔ), 介紹了一種OpenAtom OpenHarmony(以下簡稱“OpenHarmony”)輕量系統(tǒng)適配方案。
    的頭像 發(fā)表于 03-05 09:24 ?1273次閱讀
    介紹一種OpenAtom <b class='flag-5'>OpenHarmony</b>輕量系統(tǒng)適配方案
    HG百家乐官网大转轮| 百家乐官网方法技巧| 大发888游戏平台hg dafa 888 gw 大发888游戏平台dafa 888 gw | 百家乐游戏作弊| 皇家百家乐官网的玩法技巧和规则| 百家乐官网辅助分析软件| tt娱乐城怎么样| 威尼斯人娱乐城演唱会| 百家乐霸王闲| 虎在什么方位做生意好| 百家乐官网园云鼎娱乐网| 挖掘百家乐官网赢钱秘籍| 新利国际网上娱乐| 大发888娱乐场118| 威尼斯人娱乐城正规吗| 金榜百家乐娱乐城| 百家乐娱乐全讯网| 澳门百家乐官网官网站| 信誉好百家乐官网平台| 百家乐官网娱乐城7| 99真人娱乐城| 杰克棋牌是真的吗| 大发888娱乐场168| 悍马百家乐的玩法技巧和规则| 张家港百家乐赌博| 百家乐免费是玩| 巴厘岛百家乐官网的玩法技巧和规则| 赌场百家乐官网是如何玩| 百家乐官网经验博彩正网| 保德县| 必胜娱乐| 老虎机游戏| 世界德州扑克大赛| 百家乐游戏机说明书| 属鼠跟属虎做生意| 单机百家乐官网破解方法| 百家乐官网棋牌官网| 白沙| 巧家县| 凤阳县| 海盐县|