在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

完善資料讓更多小伙伴認(rèn)識你,還能領(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

示例中共涉及四類彈窗:

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

日期滑動選擇器彈窗:選擇出生日期。

文本滑動選擇器彈窗:選擇性別。

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

說明:自定義彈窗可以根據(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)。

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

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

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

開發(fā)步驟

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

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

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

具體代碼如下:

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

通過 start 和 end 分別設(shè)置日期區(qū)間的起始時間和末尾時間;通過 lunar 設(shè)置使用農(nóng)歷還是陽歷;使用 onAccept 監(jiān)聽選擇的日期,本例中通過變量 selectedDate 將選中的日期設(shè)置給參數(shù) selected,這樣彈窗彈出時的日期就默認(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)文本滑動選擇器彈窗

通過 range 設(shè)置文本選擇項(xiàng),使用 onAccept 監(jiān)聽選擇的文本項(xiàng),本例中通過變量 selectedGender 將選中的性別的索引設(shè)置給參數(shù) selected,這樣彈窗彈出時的性別就默認(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ù)訴求時,開發(fā)者可以自行設(shè)計彈窗的樣式。在實(shí)現(xiàn)自定義彈窗時,需要將彈窗的 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('個人信息')
.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:'個性簽名'
})
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ī)問題,請聯(lián)系本站處理。 舉報投訴
  • OpenHarmony
    +關(guān)注

    關(guān)注

    29

    文章

    3847

    瀏覽量

    18413

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

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

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點(diǎn)推薦

    《仿盒馬》app開發(fā)技術(shù)分享-- 分類模塊頂部導(dǎo)航列表彈窗(16)

    技術(shù)棧 Appgallery connect 開發(fā)準(zhǔn)備 一節(jié)我們實(shí)現(xiàn)了分類頁面的頂部導(dǎo)航欄列表,并且實(shí)現(xiàn)了首頁金剛區(qū)跟首頁導(dǎo)航欄的聯(lián)動,這一節(jié)我們實(shí)現(xiàn)導(dǎo)航欄列表的彈窗功能,需要學(xué)習(xí)的知識點(diǎn)有自定義
    發(fā)表于 06-30 10:34

    《仿盒馬》app開發(fā)技術(shù)分享-- 商品規(guī)格彈窗(11)

    技術(shù)棧 Appgallery connect 開發(fā)準(zhǔn)備 一節(jié)我們實(shí)現(xiàn)了商品詳情頁面,并且成功頁面上展示了商品的圖片、商品規(guī)格、活動詳情等信息,要知道同一種商品大多數(shù)都是有多種型號跟規(guī)格的,所以
    發(fā)表于 06-30 09:15

    HarmonyOS實(shí)戰(zhàn):首頁多彈窗順序彈出終極解決方案

    背景 隨著應(yīng)用軟件功能的不斷增加,應(yīng)用程序軟件首頁成為彈窗的重災(zāi)區(qū),不僅有升級彈窗,還有積分彈窗,簽到,引導(dǎo)等各種彈窗。為了徹底解彈窗問題,
    的頭像 發(fā)表于 06-09 16:47 ?129次閱讀
    HarmonyOS實(shí)戰(zhàn):首頁多<b class='flag-5'>彈窗</b>順序彈出終極解決方案

    如何在KaihongOS操作系統(tǒng)寫一個彈窗組件

    寫一個彈窗組件 KaihongOS框架提供了彈窗的API接口,開發(fā)者可直接使用,詳情請參考@ohos.promptAction (彈窗)。但在開發(fā)過程中當(dāng)提供的彈窗接口無法滿足需求時
    發(fā)表于 04-30 06:44

    請問下,openharmony支持哪一款龍芯的開發(fā)板?有沒有開源的龍芯的openharmony源碼?

    想買個2k0300的開發(fā)板學(xué)習(xí)龍芯和openharmony,愣是沒有看到提供openharmony源碼的,也沒與看到開源的代碼。giteeopenharmony的龍芯sig倉庫也是
    發(fā)表于 04-26 13:06

    DialogHub上線OpenHarmony開源社區(qū),高效開發(fā)鴻蒙應(yīng)用彈窗

    作為鴻蒙應(yīng)用開發(fā)者,使用ArkUI現(xiàn)有能力進(jìn)行彈窗開發(fā)時,總會遇到一些讓人糾結(jié)的交互問題:應(yīng)用內(nèi)進(jìn)行消息提示時,既要求消息內(nèi)容支持圖文混排,又要求彈窗本身不能打斷用戶交互(頁面滑動、頁面
    發(fā)表于 04-03 17:30

    STM32H5使用fatfs寫函數(shù)時用不了DMA的寫方式,應(yīng)該怎么使用呢?

    請問STM32H5使用fatfs寫函數(shù)時,用不了DMA的寫方式,應(yīng)該怎么使用呢,有人遇到過類似的問題嘛
    發(fā)表于 03-12 07:10

    OpenHarmony應(yīng)用與游戲開發(fā)領(lǐng)域的前沿成果

    日前,由開放原子開源基金會主辦的第二屆OpenHarmony創(chuàng)新應(yīng)用挑戰(zhàn)賽決賽路演北京圓滿結(jié)束,作為第二屆開放原子大賽的重要賽項(xiàng)之一,本屆賽事匯聚全球418支團(tuán)隊(duì),產(chǎn)出超過110個創(chuàng)新作品,集中
    的頭像 發(fā)表于 03-03 15:04 ?575次閱讀

    蜂鳥板Openharmony系統(tǒng)跑QT程序

    將QT程序放到Openharmony系統(tǒng)跑,可以運(yùn)行,但是會被覆蓋掉。(用的網(wǎng)盤里面的install,支持QT組件的版本)。 運(yùn)行情況是,終端運(yùn)行QT程序,可以正常運(yùn)行出來,但是觸摸屏幕后,會被
    發(fā)表于 02-26 13:04

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

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

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

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

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

    11月27日,OpenHarmony人才生態(tài)大會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 ?820次閱讀
    基于ArkTS語言的<b class='flag-5'>OpenHarmony</b> APP應(yīng)用開發(fā):Hello<b class='flag-5'>Openharmony</b>

    河南大學(xué)OpenHarmony技術(shù)俱樂部正式揭牌成立

    8月30日,由OpenAtom OpenHarmony(以下簡稱“OpenHarmony”)項(xiàng)目群技術(shù)指導(dǎo)委員會與河南大學(xué)共同舉辦的“河南大學(xué)OpenHarmony技術(shù)俱樂部成立大會”
    的頭像 發(fā)表于 09-03 16:12 ?712次閱讀
    河南大學(xué)<b class='flag-5'>OpenHarmony</b>技術(shù)俱樂部正式揭牌成立

    第二屆大會回顧第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 ?1183次閱讀
    第二屆大會回顧第25期 | <b class='flag-5'>OpenHarmony</b><b class='flag-5'>上</b>的Python設(shè)備應(yīng)用開發(fā)
    主站蜘蛛池模板: 全亚洲最大的777io影院 | 四虎影院永久网站 | 在线黄视频网站 | 国产一卡二卡3卡4卡四卡在线视频 | 成年色黄大色黄大片 视频 成年视频xxxxx免费播放软件 | 婷婷六月激情 | 激情六月丁香 | 欧美3d成人动画在线 | 天天干天天干 | 天天色综合1 | 国产黄网站 | 亚洲成a人不卡在线观看 | 日本三级视频在线 | 久久久婷婷亚洲5月97色 | 在线播放交视频 | 看免费黄色大片 | 欧美精品一区二区三区视频 | 最近2018中文字幕2019高清 | 三级网站免费看 | 国产色系视频在线观看免费 | 性夜黄 a 爽免费看 性夜黄a爽影免费看 | 国产精品美女一区二区三区 | 米奇777四色精品人人爽 | 日本三级成人午夜视频网 | 韩国免费三片在线视频 | 日本高清视频色www在线观看 | 欧美视频小说 | 亚洲伊人电影 | 日本久久久| 欧美67194| 天堂在线免费 | 六月激情婷婷 | 国产精品一区在线观看你懂的 | 免费看美女毛片 | 日本高清黄色 | 国产成人免费无庶挡视频 | 日韩1| 狠狠插狠狠插 | 又黄又粗暴的120秒免费gif视频 | 天天综合久久 | 久久婷婷午色综合夜啪 |