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

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

Z
zengyawen 已提交
6

G
gmy 已提交
7
通过CustomDialogController类显示自定义弹窗。使用弹窗组件时,可优先考虑自定义弹窗,便于自定义弹窗的样式与内容。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10 11

## 接口

H
HelloCrease 已提交
12
CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean, alignment?: DialogAlignment, offset?: Offset, customStyle?: boolean})
Z
zengyawen 已提交
13 14 15 16 17 18 19 20 21 22 23


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


41
## CustomDialogController
Z
zengyawen 已提交
42

43
### 导入对象
Z
zengyawen 已提交
44 45 46 47 48

```
dialogController : CustomDialogController = new CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean})
```

49 50
### open()
open(): void
Z
zengyawen 已提交
51

Z
zengyawen 已提交
52 53 54

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

Z
zengyawen 已提交
55

56 57
### close
close(): void
Z
zengyawen 已提交
58

Z
zengyawen 已提交
59 60 61

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

Z
zengyawen 已提交
62 63

## 示例
Z
zengyawen 已提交
64 65

```
T
tianyu 已提交
66
// xxx.ets
Z
zengyawen 已提交
67 68
@CustomDialog
struct CustomDialogExample {
T
tianyu 已提交
69 70
  @Link textValue: string
  @Link inputValue: string
Z
zengyawen 已提交
71 72 73 74 75 76
  controller: CustomDialogController
  cancel: () => void
  confirm: () => void

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

@Entry
@Component
struct CustomDialogUser {
T
tianyu 已提交
103 104
  @State textValue: string = ''
  @State inputValue: string = 'click me'
Z
zengyawen 已提交
105
  dialogController: CustomDialogController = new CustomDialogController({
T
tianyu 已提交
106
    builder: CustomDialogExample({ cancel: this.onCancel, confirm: this.onAccept, textValue: $textValue, inputValue: $inputValue }),
Z
zengyawen 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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 已提交
123
      Button(this.inputValue)
Z
zengyawen 已提交
124 125 126 127 128 129 130 131
        .onClick(() => {
          this.dialogController.open()
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
```

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