Popup控制
給組件綁定popup彈窗,并設(shè)置彈窗內(nèi)容,交互邏輯和顯示狀態(tài)。
說(shuō)明:
開(kāi)發(fā)前請(qǐng)熟悉鴻蒙開(kāi)發(fā)指導(dǎo)文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]
從API Version 7開(kāi)始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
接口
| 名稱(chēng) | 參數(shù)類(lèi)型 | 描述 |
|---|---|---|
| bindPopup | show: boolean, popup: [PopupOptions] | [CustomPopupOptions]8+ |
PopupOptions類(lèi)型說(shuō)明
| 名稱(chēng) | 類(lèi)型 | 必填 | 描述 |
|---|---|---|---|
| message | string | 是 | 彈窗信息內(nèi)容。 |
| primaryButton | { value: string, action: () => void } | 否 | 第一個(gè)按鈕。 value: 彈窗里主按鈕的文本。 action: 點(diǎn)擊主按鈕的回調(diào)函數(shù)。 |
| secondaryButton | { value: string, action: () => void } | 否 | 第二個(gè)按鈕。 value: 彈窗里輔助按鈕的文本。 action: 點(diǎn)擊輔助按鈕的回調(diào)函數(shù)。 |
| onStateChange | (event: { isVisible: boolean }) => void | 否 | 彈窗狀態(tài)變化事件回調(diào),參數(shù)isVisible為彈窗當(dāng)前的顯示狀態(tài)。 |
| arrowOffset9+ | [Length] | 否 | popup箭頭在彈窗處的偏移。箭頭在氣泡上下方時(shí),數(shù)值為0表示箭頭居最左側(cè),偏移量為箭頭至最左側(cè)的距離,默認(rèn)居中。箭頭在氣泡左右側(cè)時(shí),偏移量為箭頭至最上側(cè)的距離,默認(rèn)居中。如果顯示在屏幕邊緣,氣泡會(huì)自動(dòng)左右偏移,數(shù)值為0時(shí)箭頭始終指向綁定組件。 |
| showInSubWindow9+ | boolean | 否 | 是否在子窗口顯示氣泡,默認(rèn)值為false。 |
| mask10+ | boolean | [ResourceColor] | 否 |
| messageOptions10+ | [PopupMessageOptions] | 否 | 設(shè)置彈窗信息文本參數(shù)。 |
| targetSpace10+ | [Length] | 否 | 設(shè)置popup與目標(biāo)的間隙。 |
| placement10+ | [Placement] | 否 | 設(shè)置popup組件相對(duì)于目標(biāo)的顯示位置,默認(rèn)值為Placement.Bottom。 |
| offset10+ | [Position] | 否 | 設(shè)置popup組件相對(duì)于placement設(shè)置的顯示位置的偏移。**說(shuō)明:**不支持設(shè)置百分比。 |
| enableArrow10+ | boolean | 否 | 設(shè)置是否顯示箭頭。 默認(rèn)值:true |
PopupMessageOptions10+類(lèi)型說(shuō)明
| 名稱(chēng) | 類(lèi)型 | 必填 | 描述 |
|---|---|---|---|
| textColor | [ResourceColor] | 否 | 設(shè)置彈窗信息文本顏色。 |
| font | [Font] | 否 | 設(shè)置彈窗信息字體屬性。 |
CustomPopupOptions8+類(lèi)型說(shuō)明
| 名稱(chēng) | 類(lèi)型 | 必填 | 描述 |
|---|---|---|---|
| builder | [CustomBuilder] | 是 | 提示氣泡內(nèi)容的構(gòu)造器。**說(shuō)明:**popup為通用屬性,自定義popup中不支持再次彈出popup。對(duì)builder下的第一層容器組件不支持使用position屬性,如果使用將導(dǎo)致氣泡不顯示。 |
| placement | [Placement] | 否 | 氣泡組件優(yōu)先顯示的位置,當(dāng)前位置顯示不下時(shí),會(huì)自動(dòng)調(diào)整位置。 默認(rèn)值:Placement.Bottom |
| popupColor | [ResourceColor] | 否 | 提示氣泡的顏色。 默認(rèn)值:'#4d4d4d' |
| enableArrow | boolean | 否 | 是否顯示箭頭。 從API Version 9開(kāi)始,如果箭頭所在方位側(cè)的氣泡長(zhǎng)度不足以顯示下箭頭,則會(huì)默認(rèn)不顯示箭頭。比如:placement設(shè)置為L(zhǎng)eft,此時(shí)如果氣泡高度小于箭頭的寬度(32vp)與氣泡圓角兩倍(48vp)之和(80vp),則實(shí)際不會(huì)顯示箭頭。 默認(rèn)值:true |
| autoCancel | boolean | 否 | 頁(yè)面有操作時(shí),是否自動(dòng)關(guān)閉氣泡。 默認(rèn)值:true |
| onStateChange | (event: { isVisible: boolean }) => void | 否 | 彈窗狀態(tài)變化事件回調(diào),參數(shù)為彈窗當(dāng)前的顯示狀態(tài)。 |
| arrowOffset9+ | [Length] | 否 | popup箭頭在彈窗處的偏移。箭頭在氣泡上下方時(shí),數(shù)值為0表示箭頭居最左側(cè),偏移量為箭頭至最左側(cè)的距離,默認(rèn)居中。箭頭在氣泡左右側(cè)時(shí),偏移量為箭頭至最上側(cè)的距離,默認(rèn)居中。如果顯示在屏幕邊緣,氣泡會(huì)自動(dòng)左右偏移,數(shù)值為0時(shí)箭頭始終指向綁定組件。 |
| showInSubWindow9+ | boolean | 否 | 是否在子窗口顯示氣泡,默認(rèn)值為false。 |
| mask10+ | boolean | [ResourceColor] | 否 |
| targetSpace10+ | [Length] | 否 | 設(shè)置popup與目標(biāo)的間隙。 |
| offset10+ | [Position] | 否 | 設(shè)置popup組件相對(duì)于placement設(shè)置的顯示位置的偏移。**說(shuō)明:**不支持設(shè)置百分比。 |
示例
示例1
// xxx.ets
@Entry
@Component
struct PopupExample {
@State handlePopup: boolean = false
@State customPopup: boolean = false
// popup構(gòu)造器定義彈框內(nèi)容
@Builder popupBuilder() {
Row({ space: 2 }) {
Image($r("app.media.image")).width(24).height(24).margin({ left: -5 })
Text('Custom Popup').fontSize(10)
}.width(100).height(50).padding(5)
}
build() {
Flex({ direction: FlexDirection.Column }) {
// PopupOptions 類(lèi)型設(shè)置彈框內(nèi)容
Button('PopupOptions')
.onClick(() = > {
this.handlePopup = !this.handlePopup
})
.bindPopup(this.handlePopup, {
message: 'This is a popup with PopupOptions',
showInSubWindow:false,
primaryButton: {
value: 'confirm',
action: () = > {
this.handlePopup = !this.handlePopup
console.info('confirm Button click')
}
},
// 第二個(gè)按鈕
secondaryButton: {
value: 'cancel',
action: () = > {
this.handlePopup = !this.handlePopup
console.info('cancel Button click')
}
},
onStateChange: (e) = > {
console.info(JSON.stringify(e.isVisible))
if (!e.isVisible) {
this.handlePopup = false
}
}
})
.position({ x: 100, y: 50 })
// CustomPopupOptions 類(lèi)型設(shè)置彈框內(nèi)容
Button('CustomPopupOptions')
.onClick(() = > {
this.customPopup = !this.customPopup
})
.bindPopup(this.customPopup, {
builder: this.popupBuilder,
placement: Placement.Top,
mask: {color:'0x33000000'},
popupColor: Color.Yellow,
enableArrow: true,
showInSubWindow: false,
onStateChange: (e) = > {
if (!e.isVisible) {
this.customPopup = false
}
}
})
.position({ x: 80, y: 200 })
}.width('100%').padding({ top: 5 })
}
}

示例2
// xxx.ets
@Entry
@Component
struct PopupExample {
@State handlePopup: boolean = false
build() {
Column() {
Button('PopupOptions')
.onClick(() = > {
this.handlePopup = !this.handlePopup
})
.bindPopup(this.handlePopup, {
message: 'This is a popup with PopupOptions',
messageOptions: {
textColor: Color.Red,
font: {
size: '14vp',
style: FontStyle.Italic,
weight: FontWeight.Bolder
}
},
placement: Placement.Bottom,
enableArrow: false,
targetSpace: '15vp',
onStateChange: (e) = > {
console.info(JSON.stringify(e.isVisible))
if (!e.isVisible) {
this.handlePopup = false
}
}
})
}.margin(20)
}
}
`HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿`


示例3
// xxx.ets
@Entry
@Component
struct PopupExample {
@State customPopup: boolean = false
// popup構(gòu)造器定義彈框內(nèi)容
@Builder popupBuilder() {
Row() {
Text('Custom Popup Message').fontSize(10)
}.height(50).padding(5)
}
build() {
Column() {
// CustomPopupOptions 類(lèi)型設(shè)置彈框內(nèi)容
Button('CustomPopupOptions')
.onClick(() = > {
this.customPopup = !this.customPopup
})
.bindPopup(this.customPopup, {
builder: this.popupBuilder,
targetSpace: '15vp',
enableArrow: false,
onStateChange: (e) = > {
if (!e.isVisible) {
this.customPopup = false
}
}
})
}.margin(20)
}
}

審核編輯 黃宇
聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。
舉報(bào)投訴
-
Popup
+關(guān)注
關(guān)注
0文章
5瀏覽量
1443 -
鴻蒙
+關(guān)注
關(guān)注
60文章
2923瀏覽量
45566
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
熱點(diǎn)推薦
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【圖片邊框設(shè)置】 通用屬性
從API Version 9開(kāi)始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
HarmonyOS/OpenHarmony應(yīng)用開(kāi)發(fā)-ArkTS的聲明式開(kāi)發(fā)范式
基于ArkTS的聲明式開(kāi)發(fā)范式的方舟開(kāi)發(fā)框架是一套開(kāi)發(fā)極簡(jiǎn)、高性能、
發(fā)表于 01-17 15:09
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【按鍵事件】
按鍵事件指組件與鍵盤(pán)、遙控器等按鍵設(shè)備交互時(shí)觸發(fā)的事件,適用于所有可獲焦組件,例如Button。對(duì)于Text,Image等默認(rèn)不可獲焦的組件,可以設(shè)置focusable屬性為true后使用按鍵事件。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【邊框設(shè)置】 通用屬性
從API Version 7開(kāi)始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【背景設(shè)置】 通用屬性
從API Version 7開(kāi)始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【透明度設(shè)置】 通用屬性
從API Version 7開(kāi)始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【顯隱控制】 通用屬性
控制當(dāng)前組件顯示或隱藏。注意,即使組件處于隱藏狀態(tài),在頁(yè)面刷新時(shí)仍存在重新創(chuàng)建過(guò)程,因此當(dāng)對(duì)性能有嚴(yán)格要求時(shí)建議使用[條件渲染]代替。 默認(rèn)值:Visibility.Visible 從API version 9開(kāi)始,該接口支持在ArkT
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【形狀裁剪】 通用屬性
參數(shù)為相應(yīng)類(lèi)型的組件,按指定的形狀對(duì)當(dāng)前組件進(jìn)行裁剪;參數(shù)為boolean類(lèi)型時(shí),設(shè)置是否按照父容器邊緣輪廓進(jìn)行裁剪。 默認(rèn)值:false 從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【柵格設(shè)置】 通用屬性
默認(rèn)占用列數(shù),指useSizeType屬性沒(méi)有設(shè)置對(duì)應(yīng)尺寸的列數(shù)(span)時(shí),占用的柵格列數(shù)。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【菜單控制】 通用屬性
為組件綁定彈出式菜單,彈出式菜單以垂直列表形式顯示菜單項(xiàng),可通過(guò)長(zhǎng)按、點(diǎn)擊或鼠標(biāo)右鍵觸發(fā)。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【組件標(biāo)識(shí)】 通用屬性
id為組件的唯一標(biāo)識(shí),在整個(gè)應(yīng)用內(nèi)唯一。本模塊提供組件標(biāo)識(shí)相關(guān)接口,可以獲取指定id組件的屬性,也提供向指定id組件發(fā)送事件的功能。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【多態(tài)樣式】 通用屬性
設(shè)置組件不同狀態(tài)的樣式。 從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【分布式遷移標(biāo)識(shí)】 通用屬性
組件的分布式遷移標(biāo)識(shí),指明了該組件在分布式遷移場(chǎng)景下可以將特定狀態(tài)恢復(fù)到對(duì)端設(shè)備。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【無(wú)障礙屬性】 通用屬性
組件可以設(shè)置相應(yīng)的無(wú)障礙屬性和事件來(lái)更好地使用無(wú)障礙能力。
鴻蒙ArkTS聲明式開(kāi)發(fā):跨平臺(tái)支持列表【Popup控制】 通用屬性
評(píng)論