ts-basic-components-button.md 5.2 KB
Newer Older
Z
zengyawen 已提交
1 2
# Button

T
explain  
tianyu 已提交
3 4
按钮组件,可快速创建不同样式的按钮。

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

Z
zengyawen 已提交
8

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



Z
zengyawen 已提交
13 14

## 子组件
Z
zengyawen 已提交
15

Z
zhongyuyan 已提交
16
可以包含单个子组件。
Z
zengyawen 已提交
17

Z
zengyawen 已提交
18 19 20 21

## 接口

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

Z
zengyawen 已提交
23 24
  **表1** options参数说明

25 26
  | 参数名         | 参数类型       | 必填   | 默认值     | 参数描述                              |
  | ----------- | ---------- | ---- | ------- | --------------------------------- |
K
kangchongtao 已提交
27
  | type        | ButtonType | 否    | ButtonType.Capsule | 描述按钮风格。                           |
28
  | stateEffect | boolean    | 否    | true    | 按钮按下时是否开启切换效果,当状态置为false时,点击效果关闭。 |
Z
zengyawen 已提交
29

30 31

- Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean })
32

Z
zengyawen 已提交
33 34 35
  使用文本内容创建相应的按钮组件,此时Button无法包含子组件。

  **表2** value参数说明
36 37 38

  | 参数名     | 参数类型                                | 必填   | 默认值  | 参数描述          |
  | ------- | ----------------------------------- | ---- | ---- | ------------- |
L
fix doc  
luoying_ace_admin 已提交
39
  | label   | [ResourceStr](ts-types.md#resourcestr) | 否    | -    | 按钮文本内容。       |
40
  | options | Object                              | 否    | -    | 见options参数说明。 |
Z
zengyawen 已提交
41 42 43 44


## 属性

45 46
| 名称          | 参数类型       | 默认值     | 描述                                |
| ----------- | ---------- | ------- | --------------------------------- |
K
kangchongtao 已提交
47
| type        | ButtonType | ButtonType.Capsule | 设置Button样式。                       |
48
| stateEffect | boolean    | true    | 状态切换时是否开启切换效果,当状态置为false时,点击效果关闭。 |
Z
zengyawen 已提交
49 50

- ButtonType枚举说明
51 52
  | 名称      | 描述                 |
  | ------- | ------------------ |
T
explain  
tianyu 已提交
53
  | Capsule | 胶囊型按钮(圆角默认为高度的一半)。 |
54 55
  | Circle  | 圆形按钮。              |
  | Normal  | 普通按钮(默认不带圆角)。      |
Z
zengyawen 已提交
56

H
geshi  
HelloCrease 已提交
57
>  **说明:**
58 59 60 61
>  - 按钮圆角通过[通用属性borderRadius设置](ts-universal-attributes-border.md)(不支持通过border接口设置圆角)。
>  - 当按钮类型为Capsule时,borderRadius设置不生效,按钮圆角始终为高度的一半。
>  - 当按钮类型为Circle时,borderRadius即为按钮半径,若未设置borderRadius按钮半径则为宽、高中较小值的一半。
>  - 按钮文本通过[通用文本样式](ts-universal-attributes-text-style.md)进行设置。
Z
zengyawen 已提交
62 63 64


## 示例
Z
zengyawen 已提交
65

H
geshi  
HelloCrease 已提交
66 67
```ts
// xxx.ets
Z
zengyawen 已提交
68 69 70 71 72 73 74 75 76 77
@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() {
78
            // 图片建议通过$r创建Resource对象引入
Z
zengyawen 已提交
79 80 81 82 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
            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 })
  }
}
```

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