自定義彈窗

2024-01-22 18:21 更新

通過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

DialogAlignment

彈窗在豎直方向上的對(duì)齊方式。

默認(rèn)值:DialogAlignment.Default

offset

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ù)。

CustomDialogController

導(dǎo)入對(duì)象

  1. dialogController : CustomDialogController = new CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean})
說明

CustomDialogController僅在作為@CustomDialog和@Component struct的成員變量,且在@Component struct內(nèi)部定義時(shí)賦值才有效,具體用法可看下方示例。

open()

open(): void

顯示自定義彈窗內(nèi)容,允許多次使用,但如果彈框?yàn)镾ubWindow模式,則該彈框不允許再彈出SubWindow彈框。

close

close(): void

關(guān)閉顯示的自定義彈窗,若已關(guān)閉,則不生效。

示例

  1. // xxx.ets
  2. @CustomDialog
  3. struct CustomDialogExample {
  4. @Link textValue: string
  5. @Link inputValue: string
  6. controller: CustomDialogController
  7. // 若嘗試在CustomDialog中傳入多個(gè)其他的Controller,以實(shí)現(xiàn)在CustomDialog中打開另一個(gè)或另一些CustomDialog,那么此處需要將指向自己的controller放在最后
  8. cancel: () => void
  9. confirm: () => void
  10. build() {
  11. Column() {
  12. Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
  13. TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
  14. .onChange((value: string) => {
  15. this.textValue = value
  16. })
  17. Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
  18. Flex({ justifyContent: FlexAlign.SpaceAround }) {
  19. Button('cancel')
  20. .onClick(() => {
  21. this.controller.close()
  22. this.cancel()
  23. }).backgroundColor(0xffffff).fontColor(Color.Black)
  24. Button('confirm')
  25. .onClick(() => {
  26. this.inputValue = this.textValue
  27. this.controller.close()
  28. this.confirm()
  29. }).backgroundColor(0xffffff).fontColor(Color.Red)
  30. }.margin({ bottom: 10 })
  31. }
  32. // dialog默認(rèn)的borderRadius為24vp,如果需要使用border屬性,請(qǐng)和borderRadius屬性一起使用。
  33. }
  34. }
  35. @Entry
  36. @Component
  37. struct CustomDialogUser {
  38. @State textValue: string = ''
  39. @State inputValue: string = 'click me'
  40. dialogController: CustomDialogController = new CustomDialogController({
  41. builder: CustomDialogExample({
  42. cancel: this.onCancel,
  43. confirm: this.onAccept,
  44. textValue: $textValue,
  45. inputValue: $inputValue
  46. }),
  47. cancel: this.existApp,
  48. autoCancel: true,
  49. alignment: DialogAlignment.Bottom,
  50. offset: { dx: 0, dy: -20 },
  51. gridCount: 4,
  52. customStyle: false
  53. })
  54. // 在自定義組件即將析構(gòu)銷毀時(shí)將dialogController置空
  55. aboutToDisappear() {
  56. this.dialogController = undefined // 將dialogController置空
  57. }
  58. onCancel() {
  59. console.info('Callback when the first button is clicked')
  60. }
  61. onAccept() {
  62. console.info('Callback when the second button is clicked')
  63. }
  64. existApp() {
  65. console.info('Click the callback in the blank area')
  66. }
  67. build() {
  68. Column() {
  69. Button(this.inputValue)
  70. .onClick(() => {
  71. if (this.dialogController != undefined) {
  72. this.dialogController.open()
  73. }
  74. }).backgroundColor(0x317aff)
  75. }.width('100%').margin({ top: 5 })
  76. }
  77. }

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)