ts-methods-custom-dialog-box.md 4.8 KB
Newer Older
Z
zengyawen 已提交
1 2
# 自定义弹窗

3
通过CustomDialogController类显示自定义弹窗。使用弹窗组件时,可优先考虑自定义弹窗,便于自定义弹窗的样式与内容。
4

H
geshi  
HelloCrease 已提交
5
> **说明:**
6
>
Z
zengyawen 已提交
7
> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10


Z
zengyawen 已提交
11 12 13

## 接口

L
fix doc  
luoying_ace_admin 已提交
14
CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean, alignment?: DialogAlignment, offset?: Offset, customStyle?: boolean, gridCount?: number})
Z
zengyawen 已提交
15 16


17 18 19 20 21 22
**参数:**

| 参数名                    | 参数类型                                     | 必填                  | 参数描述                   |
| ---------------------- | ---------------------------------------- | ------------------------- | ---------------------- |
| builder                | [CustomDialog](../../ui/ts-component-based-customdialog.md) | 是     | 自定义弹窗内容构造器。            |
| cancel                 | () => void                            | 否              | 点击遮障层退出时的回调。           |
23
| autoCancel             | boolean                                            | 否              | 是否允许点击遮障层退出。<br>默认值:true           |
24
| alignment              | [DialogAlignment](#dialogalignment枚举说明)           | 否              | 弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Default        |
25
| offset                 | {<br/>dx:&nbsp;Length&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>dy:&nbsp;Length&nbsp;&nbsp;\|&nbsp;[Resource](ts-types.md#resource)<br/>} | 否    | 弹窗相对alignment所在位置的偏移量。 |
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| customStyle            | boolean                                  | 否                    | 弹窗容器样式是否自定义。<br>默认值:false           |
| gridCount<sup>8+</sup> | number                                   | 否                    | 弹窗宽度占栅格宽度的个数。              |

## DialogAlignment枚举说明

| 名称                       | 描述      |
| ------------------------ | ------- |
| Top                      | 垂直顶部对齐。 |
| Center                   | 垂直居中对齐。 |
| Bottom                   | 垂直底部对齐。 |
| Default                  | 默认对齐。   |
| TopStart<sup>8+</sup>    | 左上对齐。   |
| TopEnd<sup>8+</sup>      | 右上对齐。   |
| CenterStart<sup>8+</sup> | 左中对齐。   |
| CenterEnd<sup>8+</sup>   | 右中对齐。   |
| BottomStart<sup>8+</sup> | 左下对齐。   |
| BottomEnd<sup>8+</sup>   | 右下对齐。   |
Z
zengyawen 已提交
43 44


45
## CustomDialogController
Z
zengyawen 已提交
46

47
### 导入对象
Z
zengyawen 已提交
48

49
```ts
Z
zengyawen 已提交
50 51 52
dialogController : CustomDialogController = new CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean})
```

53 54
### open()
open(): void
Z
zengyawen 已提交
55

Z
zengyawen 已提交
56 57 58

显示自定义弹窗内容,若已显示,则不生效。

Z
zengyawen 已提交
59

60 61
### close
close(): void
Z
zengyawen 已提交
62

Z
zengyawen 已提交
63 64 65

关闭显示的自定义弹窗,若已关闭,则不生效。

Z
zengyawen 已提交
66 67

## 示例
Z
zengyawen 已提交
68

H
geshi  
HelloCrease 已提交
69 70
```ts
// xxx.ets
Z
zengyawen 已提交
71 72
@CustomDialog
struct CustomDialogExample {
T
tianyu 已提交
73 74
  @Link textValue: string
  @Link inputValue: string
Z
zengyawen 已提交
75 76 77 78 79 80
  controller: CustomDialogController
  cancel: () => void
  confirm: () => void

  build() {
    Column() {
T
tianyu 已提交
81 82 83 84 85 86
      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 })
Z
zengyawen 已提交
87 88 89 90 91 92 93 94
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            this.controller.close()
            this.cancel()
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
T
tianyu 已提交
95
            this.inputValue = this.textValue
Z
zengyawen 已提交
96 97 98 99 100 101 102 103 104 105 106
            this.controller.close()
            this.confirm()
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }
  }
}

@Entry
@Component
struct CustomDialogUser {
T
tianyu 已提交
107 108
  @State textValue: string = ''
  @State inputValue: string = 'click me'
Z
zengyawen 已提交
109
  dialogController: CustomDialogController = new CustomDialogController({
T
tianyu 已提交
110
    builder: CustomDialogExample({ cancel: this.onCancel, confirm: this.onAccept, textValue: $textValue, inputValue: $inputValue }),
Z
zengyawen 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    cancel: this.existApp,
    autoCancel: true
  })

  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() {
T
tianyu 已提交
127
      Button(this.inputValue)
Z
zengyawen 已提交
128 129 130 131 132 133 134 135
        .onClick(() => {
          this.dialogController.open()
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
```

Z
zengyawen 已提交
136
![zh-cn_image_0000001219744203](figures/zh-cn_image_0000001219744203.gif)