ts-basic-components-button.md 9.7 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4
# Button<a name="ZH-CN_TOPIC_0000001192755124"></a>

>![](../../public_sys-resources/icon-note.gif) **说明:** 
>该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

提供按钮组件。

## 权限列表<a name="section173151571452"></a>



## 子组件<a name="section13412913174619"></a>

可以包含子组件。

## 接口<a name="section124082310463"></a>

-   Button\(options?: \{type?: ButtonType, stateEffect?: boolean\}\)

    **表 1**  options参数说明

    <a name="table712610266288"></a>
Z
zengyawen 已提交
23
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="16.11%" id="mcps1.2.6.1.1"><p>参数名</p>
Z
zengyawen 已提交
24
    </th>
Z
zengyawen 已提交
25
    <th class="cellrowborder" valign="top" width="15.47%" id="mcps1.2.6.1.2"><p>参数类型</p>
Z
zengyawen 已提交
26
    </th>
Z
zengyawen 已提交
27
    <th class="cellrowborder" valign="top" width="13.03%" id="mcps1.2.6.1.3"><p>必填</p>
Z
zengyawen 已提交
28
    </th>
Z
zengyawen 已提交
29
    <th class="cellrowborder" valign="top" width="13.18%" id="mcps1.2.6.1.4"><p>默认值</p>
Z
zengyawen 已提交
30
    </th>
Z
zengyawen 已提交
31
    <th class="cellrowborder" valign="top" width="42.21%" id="mcps1.2.6.1.5"><p>参数描述</p>
Z
zengyawen 已提交
32 33 34
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
35
    <tbody><tr><td class="cellrowborder" valign="top" width="16.11%" headers="mcps1.2.6.1.1 "><p>type</p>
Z
zengyawen 已提交
36
    </td>
Z
zengyawen 已提交
37
    <td class="cellrowborder" valign="top" width="15.47%" headers="mcps1.2.6.1.2 "><p>ButtonType</p>
Z
zengyawen 已提交
38
    </td>
Z
zengyawen 已提交
39
    <td class="cellrowborder" valign="top" width="13.03%" headers="mcps1.2.6.1.3 "><p>否</p>
Z
zengyawen 已提交
40
    </td>
Z
zengyawen 已提交
41
    <td class="cellrowborder" valign="top" width="13.18%" headers="mcps1.2.6.1.4 "><p>Capsule</p>
Z
zengyawen 已提交
42
    </td>
Z
zengyawen 已提交
43
    <td class="cellrowborder" valign="top" width="42.21%" headers="mcps1.2.6.1.5 "><p>描述按钮风格。</p>
Z
zengyawen 已提交
44 45
    </td>
    </tr>
Z
zengyawen 已提交
46
    <tr><td class="cellrowborder" valign="top" width="16.11%" headers="mcps1.2.6.1.1 "><p>stateEffect</p>
Z
zengyawen 已提交
47
    </td>
Z
zengyawen 已提交
48
    <td class="cellrowborder" valign="top" width="15.47%" headers="mcps1.2.6.1.2 "><p>boolean</p>
Z
zengyawen 已提交
49
    </td>
Z
zengyawen 已提交
50
    <td class="cellrowborder" valign="top" width="13.03%" headers="mcps1.2.6.1.3 "><p>否</p>
Z
zengyawen 已提交
51
    </td>
Z
zengyawen 已提交
52
    <td class="cellrowborder" valign="top" width="13.18%" headers="mcps1.2.6.1.4 "><p>true</p>
Z
zengyawen 已提交
53
    </td>
Z
zengyawen 已提交
54
    <td class="cellrowborder" valign="top" width="42.21%" headers="mcps1.2.6.1.5 "><p>按钮按下时是否开启切换效果,当状态置为false时,点击效果关闭。</p>
Z
zengyawen 已提交
55 56 57 58 59 60 61 62 63 64 65 66
    </td>
    </tr>
    </tbody>
    </table>


-   Button\(label?: string, options?: \{ type?: ButtonType, stateEffect?: boolean \}\)

    使用文本内容创建相应的按钮组件,此时Button无法包含子组件。

    **表 2**  value参数说明

Z
zengyawen 已提交
67
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="16.11%" id="mcps1.2.6.1.1"><p>参数名</p>
Z
zengyawen 已提交
68
    </th>
Z
zengyawen 已提交
69
    <th class="cellrowborder" valign="top" width="14.01%" id="mcps1.2.6.1.2"><p>参数类型</p>
Z
zengyawen 已提交
70
    </th>
Z
zengyawen 已提交
71
    <th class="cellrowborder" valign="top" width="14.49%" id="mcps1.2.6.1.3"><p>必填</p>
Z
zengyawen 已提交
72
    </th>
Z
zengyawen 已提交
73
    <th class="cellrowborder" valign="top" width="13.18%" id="mcps1.2.6.1.4"><p>默认值</p>
Z
zengyawen 已提交
74
    </th>
Z
zengyawen 已提交
75
    <th class="cellrowborder" valign="top" width="42.21%" id="mcps1.2.6.1.5"><p>参数描述</p>
Z
zengyawen 已提交
76 77 78
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
79
    <tbody><tr><td class="cellrowborder" valign="top" width="16.11%" headers="mcps1.2.6.1.1 "><p>label</p>
Z
zengyawen 已提交
80
    </td>
Z
zengyawen 已提交
81
    <td class="cellrowborder" valign="top" width="14.01%" headers="mcps1.2.6.1.2 "><p>string</p>
Z
zengyawen 已提交
82
    </td>
Z
zengyawen 已提交
83
    <td class="cellrowborder" valign="top" width="14.49%" headers="mcps1.2.6.1.3 "><p>否</p>
Z
zengyawen 已提交
84
    </td>
Z
zengyawen 已提交
85
    <td class="cellrowborder" valign="top" width="13.18%" headers="mcps1.2.6.1.4 "><p>-</p>
Z
zengyawen 已提交
86
    </td>
Z
zengyawen 已提交
87
    <td class="cellrowborder" valign="top" width="42.21%" headers="mcps1.2.6.1.5 "><p>按钮文本内容。</p>
Z
zengyawen 已提交
88 89
    </td>
    </tr>
Z
zengyawen 已提交
90
    <tr><td class="cellrowborder" valign="top" width="16.11%" headers="mcps1.2.6.1.1 "><p>options</p>
Z
zengyawen 已提交
91
    </td>
Z
zengyawen 已提交
92
    <td class="cellrowborder" valign="top" width="14.01%" headers="mcps1.2.6.1.2 "><p>Object</p>
Z
zengyawen 已提交
93
    </td>
Z
zengyawen 已提交
94
    <td class="cellrowborder" valign="top" width="14.49%" headers="mcps1.2.6.1.3 "><p>否</p>
Z
zengyawen 已提交
95
    </td>
Z
zengyawen 已提交
96
    <td class="cellrowborder" valign="top" width="13.18%" headers="mcps1.2.6.1.4 "><p>-</p>
Z
zengyawen 已提交
97
    </td>
Z
zengyawen 已提交
98
    <td class="cellrowborder" valign="top" width="42.21%" headers="mcps1.2.6.1.5 "><p>见<a href="#table712610266288">options参数说明</a>。</p>
Z
zengyawen 已提交
99 100 101 102 103 104 105 106
    </td>
    </tr>
    </tbody>
    </table>


## 属性<a name="section94181084473"></a>

Z
zengyawen 已提交
107
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="13.389999999999999%" id="mcps1.1.5.1.1"><p>名称</p>
Z
zengyawen 已提交
108
</th>
Z
zengyawen 已提交
109
<th class="cellrowborder" valign="top" width="14.430000000000001%" id="mcps1.1.5.1.2"><p>参数类型</p>
Z
zengyawen 已提交
110
</th>
Z
zengyawen 已提交
111
<th class="cellrowborder" valign="top" width="11.87%" id="mcps1.1.5.1.3"><p>默认值</p>
Z
zengyawen 已提交
112
</th>
Z
zengyawen 已提交
113
<th class="cellrowborder" valign="top" width="60.309999999999995%" id="mcps1.1.5.1.4"><p>描述</p>
Z
zengyawen 已提交
114 115 116
</th>
</tr>
</thead>
Z
zengyawen 已提交
117
<tbody><tr><td class="cellrowborder" valign="top" width="13.389999999999999%" headers="mcps1.1.5.1.1 "><p>type</p>
Z
zengyawen 已提交
118
</td>
Z
zengyawen 已提交
119
<td class="cellrowborder" valign="top" width="14.430000000000001%" headers="mcps1.1.5.1.2 "><p><a href="#li93236107910">ButtonType</a></p>
Z
zengyawen 已提交
120
</td>
Z
zengyawen 已提交
121
<td class="cellrowborder" valign="top" width="11.87%" headers="mcps1.1.5.1.3 "><p>Capsule</p>
Z
zengyawen 已提交
122
</td>
Z
zengyawen 已提交
123
<td class="cellrowborder" valign="top" width="60.309999999999995%" headers="mcps1.1.5.1.4 "><p>设置Button样式。</p>
Z
zengyawen 已提交
124 125
</td>
</tr>
Z
zengyawen 已提交
126
<tr><td class="cellrowborder" valign="top" width="13.389999999999999%" headers="mcps1.1.5.1.1 "><p>stateEffect</p>
Z
zengyawen 已提交
127
</td>
Z
zengyawen 已提交
128
<td class="cellrowborder" valign="top" width="14.430000000000001%" headers="mcps1.1.5.1.2 "><p>boolean</p>
Z
zengyawen 已提交
129
</td>
Z
zengyawen 已提交
130
<td class="cellrowborder" valign="top" width="11.87%" headers="mcps1.1.5.1.3 "><p>true</p>
Z
zengyawen 已提交
131
</td>
Z
zengyawen 已提交
132
<td class="cellrowborder" valign="top" width="60.309999999999995%" headers="mcps1.1.5.1.4 "><p>状态切换时是否开启切换效果,当状态置为false时,点击效果关闭。</p>
Z
zengyawen 已提交
133 134 135 136 137 138 139
</td>
</tr>
</tbody>
</table>

-   <a name="li93236107910"></a>ButtonType枚举说明

Z
zengyawen 已提交
140
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="25.3%" id="mcps1.1.3.1.1"><p>名称</p>
Z
zengyawen 已提交
141
    </th>
Z
zengyawen 已提交
142
    <th class="cellrowborder" valign="top" width="74.7%" id="mcps1.1.3.1.2"><p>描述</p>
Z
zengyawen 已提交
143 144 145
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
146
    <tbody><tr><td class="cellrowborder" valign="top" width="25.3%" headers="mcps1.1.3.1.1 "><p>Capsule</p>
Z
zengyawen 已提交
147
    </td>
Z
zengyawen 已提交
148
    <td class="cellrowborder" valign="top" width="74.7%" headers="mcps1.1.3.1.2 "><p>胶囊型按钮(圆角默认为高度的一半)。</p>
Z
zengyawen 已提交
149 150
    </td>
    </tr>
Z
zengyawen 已提交
151
    <tr><td class="cellrowborder" valign="top" width="25.3%" headers="mcps1.1.3.1.1 "><p>Circle</p>
Z
zengyawen 已提交
152
    </td>
Z
zengyawen 已提交
153
    <td class="cellrowborder" valign="top" width="74.7%" headers="mcps1.1.3.1.2 "><p>圆形按钮。</p>
Z
zengyawen 已提交
154 155
    </td>
    </tr>
Z
zengyawen 已提交
156
    <tr><td class="cellrowborder" valign="top" width="25.3%" headers="mcps1.1.3.1.1 "><p>Normal</p>
Z
zengyawen 已提交
157
    </td>
Z
zengyawen 已提交
158
    <td class="cellrowborder" valign="top" width="74.7%" headers="mcps1.1.3.1.2 "><p>普通按钮(默认不带圆角)。</p>
Z
zengyawen 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    </td>
    </tr>
    </tbody>
    </table>


>![](../../public_sys-resources/icon-note.gif) **说明:** 
>-   按钮圆角通过[通用属性borderRadius设置](ts-universal-attributes-border.md)(不支持通过border接口设置圆角)。
>-   按钮文本通过[通用文本样式](ts-universal-attributes-text-style.md)进行设置。

## 示例<a name="section449999124812"></a>

```
@Entry
@Component
struct ButtonExample {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
      Text('Common button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
        Button('Ok', { type: ButtonType.Normal, stateEffect: true }).borderRadius(8).backgroundColor(0x317aff).width(90)
        Button({ type: ButtonType.Normal, stateEffect: true }) {
          Row() {
            Image($r('app.media.loading')).width(20).height(20).margin({ left: 12 })
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
          }.alignItems(VerticalAlign.Center)
        }.borderRadius(8).backgroundColor(0x317aff).width(90)
        Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.5)
          .borderRadius(8).backgroundColor(0x317aff).width(90)
      }

      Text('Capsule button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
        Button('Ok', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90)
        Button({ type: ButtonType.Capsule, stateEffect: true }) {
          Row() {
            Image($r('app.media.loading')).width(20).height(20).margin({ left: 12 })
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
          }.alignItems(VerticalAlign.Center).width(90)
        }.backgroundColor(0x317aff)
        .onClick((event: ClickEvent) => {
          AlertDialog.show({ message: 'The login is successful' })
        })
        Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.5)
          .backgroundColor(0x317aff).width(90)
      }

      Text('Circle button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, wrap: FlexWrap.Wrap }) {
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r('app.media.ic_public_app_filled')).width(20).height(20)
        }.width(55).height(55).backgroundColor(0x317aff)
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r('app.media.ic_public_delete_filled')).width(30).height(30)
        }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
      }
    }.height(400).padding({ left: 35, right: 35, top: 35 })
  }
}
```

![](figures/Button.gif)