js-components-container-dialog.md 3.7 KB
Newer Older
Z
zengyawen 已提交
1 2
# dialog

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

自定义弹窗容器。

Z
zengyawen 已提交
8
## 权限列表
Z
zengyawen 已提交
9 10 11



Z
zengyawen 已提交
12 13

## 子组件
Z
zengyawen 已提交
14 15 16

支持单个子组件。

Z
zengyawen 已提交
17 18 19 20 21

## 属性

除支持[通用属性](../arkui-js/js-components-common-attributes.md)外,支持如下属性:

H
geshi  
HelloCrease 已提交
22 23 24
| 名称                    | 类型      | 默认值   | 必填   | 描述           |
| --------------------- | ------- | ----- | ---- | ------------ |
| dragable<sup>7+</sup> | boolean | false | 否    | 设置对话框是否支持拖拽。 |
Z
zengyawen 已提交
25

H
geshi  
HelloCrease 已提交
26
>  **说明:**
Z
zengyawen 已提交
27
>
H
geshi  
HelloCrease 已提交
28
>  弹窗类组件不支持focusable、click-effect属性。
Z
zengyawen 已提交
29 30 31 32 33 34 35 36 37 38 39


## 样式

仅支持[通用样式](../arkui-js/js-components-common-styles.md)中的width、height、margin、margin-[left|top|right|bottom]、margin-[start|end]样式。


## 事件

不支持[通用事件](../arkui-js/js-components-common-events.md),仅支持如下事件:

H
geshi  
HelloCrease 已提交
40 41 42 43 44
| 名称                 | 参数   | 描述                         |
| ------------------ | ---- | -------------------------- |
| cancel             | -    | 用户点击非dialog区域触发取消弹窗时触发的事件。 |
| show<sup>7+</sup>  | -    | 对话框弹出时触发该事件。               |
| close<sup>7+</sup> | -    | 对话框关闭时触发该事件。               |
Z
zengyawen 已提交
45 46 47 48 49 50


## 方法

不支持[通用方法](../arkui-js/js-components-common-methods.md),仅支持如下方法。

H
geshi  
HelloCrease 已提交
51 52 53 54
| 名称    | 参数   | 描述     |
| ----- | ---- | ------ |
| show  | -    | 弹出对话框。 |
| close | -    | 关闭对话框。 |
Z
zengyawen 已提交
55

H
geshi  
HelloCrease 已提交
56 57
>  **说明:**
>  dialog属性、样式均不支持动态更新。
Z
zengyawen 已提交
58 59 60


## 示例
Z
zengyawen 已提交
61

H
geshi  
HelloCrease 已提交
62
```html
Z
zengyawen 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
<!-- xxx.hml -->
<div class="doc-page">
  <div class="btn-div">
    <button type="capsule" value="Click here" class="btn" onclick="showDialog"></button>
  </div>
  <dialog id="simpledialog" dragable="true" class="dialog-main" oncancel="cancelDialog">
    <div class="dialog-div">
      <div class="inner-txt">
        <text class="txt" ondoubleclick="doubleclick">Simple dialog</text>
      </div>
      <div class="inner-btn">
        <button type="capsule" value="Cancel" onclick="cancelSchedule" class="btn-txt"></button>
        <button type="capsule" value="Confirm" onclick="setSchedule" class="btn-txt"></button>
      </div>
    </div>
  </dialog>
</div>
```

H
geshi  
HelloCrease 已提交
82
```css
Z
zengyawen 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/* xxx.css */
.doc-page {
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.btn-div {
  width: 100%;
  height: 200px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.btn {
  background-color: #F2F2F2;
  text-color: #0D81F2;
}
.txt {
  color: #000000;
  font-weight: bold;
  font-size: 39px;
}
.dialog-main {
  width: 500px;
}
.dialog-div {
  flex-direction: column;
  align-items: center;
}
.inner-txt {
  width: 400px;
  height: 160px;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
}
.inner-btn {
  width: 400px;
  height: 120px;
  justify-content: space-around;
  align-items: center;
}
.btn-txt {
  background-color: #F2F2F2;
  text-color: #0D81F2;
}
```

H
geshi  
HelloCrease 已提交
131
```js
Z
zengyawen 已提交
132 133 134
// xxx.js
import prompt from '@system.prompt';
export default {
135
  showDialog() {
Z
zengyawen 已提交
136 137
    this.$element('simpledialog').show()
  },
138
  cancelDialog() {
Z
zengyawen 已提交
139 140 141 142
    prompt.showToast({
      message: 'Dialog cancelled'
    })
  },
143
  cancelSchedule() {
Z
zengyawen 已提交
144 145 146 147 148
    this.$element('simpledialog').close()
    prompt.showToast({
      message: 'Successfully cancelled'
    })
  },
149
  setSchedule() {
Z
zengyawen 已提交
150 151 152 153 154
    this.$element('simpledialog').close()
    prompt.showToast({
      message: 'Successfully confirmed'
    })
  },
155
  doubleclick(){
Z
zengyawen 已提交
156 157 158 159 160 161 162
    prompt.showToast({
      message: 'doubleclick'
    })
  }
}
```

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