W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
通過CustomDialogController類顯示自定義彈窗。使用彈窗組件時(shí),可優(yōu)先考慮自定義彈窗,便于自定義彈窗的樣式與內(nèi)容。
從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean, alignment?: DialogAlignment, offset?: Offset, customStyle?: boolean, gridCount?: number})
自定義彈窗的所有參數(shù),不支持動(dòng)態(tài)刷新。
參數(shù):
參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
---|---|---|---|
builder | CustomDialog | 是 | 自定義彈窗內(nèi)容構(gòu)造器。 |
cancel | () => void | 否 | 點(diǎn)擊遮障層退出時(shí)的回調(diào)。 |
autoCancel | boolean | 否 | 是否允許點(diǎn)擊遮障層退出。 默認(rèn)值:true |
alignment | 否 | 彈窗在豎直方向上的對(duì)齊方式。 默認(rèn)值:DialogAlignment.Default | |
offset | 否 | 彈窗相對(duì)alignment所在位置的偏移量。 | |
customStyle | boolean | 否 | 彈窗容器樣式是否自定義。 默認(rèn)值:false,彈窗容器的寬度根據(jù)柵格系統(tǒng)自適應(yīng),不跟隨子節(jié)點(diǎn);高度自適應(yīng)子節(jié)點(diǎn),最大為窗口高度的90%;圓角為24vp。 |
gridCount8+ | number | 否 | 彈窗寬度占柵格寬度的個(gè)數(shù)。 默認(rèn)為按照窗口大小自適應(yīng),異常值按默認(rèn)值處理,最大柵格數(shù)為系統(tǒng)最大柵格數(shù)。 |
- dialogController : CustomDialogController = new CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean})
CustomDialogController僅在作為@CustomDialog和@Component struct的成員變量,且在@Component struct內(nèi)部定義時(shí)賦值才有效,具體用法可看下方示例。
- // xxx.ets
- @CustomDialog
- struct CustomDialogExample {
- @Link textValue: string
- @Link inputValue: string
- controller: CustomDialogController
- // 若嘗試在CustomDialog中傳入多個(gè)其他的Controller,以實(shí)現(xiàn)在CustomDialog中打開另一個(gè)或另一些CustomDialog,那么此處需要將指向自己的controller放在最后
- cancel: () => void
- confirm: () => void
- build() {
- Column() {
- Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
- TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
- .onChange((value: string) => {
- this.textValue = value
- })
- Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
- Flex({ justifyContent: FlexAlign.SpaceAround }) {
- Button('cancel')
- .onClick(() => {
- this.controller.close()
- this.cancel()
- }).backgroundColor(0xffffff).fontColor(Color.Black)
- Button('confirm')
- .onClick(() => {
- this.inputValue = this.textValue
- this.controller.close()
- this.confirm()
- }).backgroundColor(0xffffff).fontColor(Color.Red)
- }.margin({ bottom: 10 })
- }
- // dialog默認(rèn)的borderRadius為24vp,如果需要使用border屬性,請(qǐng)和borderRadius屬性一起使用。
- }
- }
- @Entry
- @Component
- struct CustomDialogUser {
- @State textValue: string = ''
- @State inputValue: string = 'click me'
- dialogController: CustomDialogController = new CustomDialogController({
- builder: CustomDialogExample({
- cancel: this.onCancel,
- confirm: this.onAccept,
- textValue: $textValue,
- inputValue: $inputValue
- }),
- cancel: this.existApp,
- autoCancel: true,
- alignment: DialogAlignment.Bottom,
- offset: { dx: 0, dy: -20 },
- gridCount: 4,
- customStyle: false
- })
- // 在自定義組件即將析構(gòu)銷毀時(shí)將dialogController置空
- aboutToDisappear() {
- this.dialogController = undefined // 將dialogController置空
- }
- onCancel() {
- console.info('Callback when the first button is clicked')
- }
- onAccept() {
- console.info('Callback when the second button is clicked')
- }
- existApp() {
- console.info('Click the callback in the blank area')
- }
- build() {
- Column() {
- Button(this.inputValue)
- .onClick(() => {
- if (this.dialogController != undefined) {
- this.dialogController.open()
- }
- }).backgroundColor(0x317aff)
- }.width('100%').margin({ top: 5 })
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: