Popup控制
給組件綁定popup彈窗,并設(shè)置彈窗內(nèi)容,交互邏輯和顯示狀態(tài)。
說明:
開發(fā)前請(qǐng)熟悉鴻蒙開發(fā)指導(dǎo)文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
接口
名稱 | 參數(shù)類型 | 描述 |
---|---|---|
bindPopup | show: boolean, popup: [PopupOptions] | [CustomPopupOptions]8+ |
PopupOptions類型說明
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
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è)置的顯示位置的偏移。**說明:**不支持設(shè)置百分比。 |
enableArrow10+ | boolean | 否 | 設(shè)置是否顯示箭頭。 默認(rèn)值:true |
PopupMessageOptions10+類型說明
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
textColor | [ResourceColor] | 否 | 設(shè)置彈窗信息文本顏色。 |
font | [Font] | 否 | 設(shè)置彈窗信息字體屬性。 |
CustomPopupOptions8+類型說明
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
builder | [CustomBuilder] | 是 | 提示氣泡內(nèi)容的構(gòu)造器。**說明:**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開始,如果箭頭所在方位側(cè)的氣泡長(zhǎng)度不足以顯示下箭頭,則會(huì)默認(rèn)不顯示箭頭。比如:placement設(shè)置為L(zhǎng)eft,此時(shí)如果氣泡高度小于箭頭的寬度(32vp)與氣泡圓角兩倍(48vp)之和(80vp),則實(shí)際不會(huì)顯示箭頭。 默認(rèn)值:true |
autoCancel | boolean | 否 | 頁面有操作時(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è)置的顯示位置的偏移。**說明:**不支持設(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 類型設(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 類型設(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 類型設(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)
}
}
審核編輯 黃宇
-
Popup
+關(guān)注
關(guān)注
0文章
5瀏覽量
1347 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2395瀏覽量
43087
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論