ui-ts-basic-components-button.md 4.8 KB
Newer Older
K
kukixi 已提交
1 2 3 4 5 6 7 8 9 10
# Button


Button是按钮组件,通常用于响应用户的点击操作,如提交表单,其类型包括胶囊按钮、圆形按钮、普通按钮。


## 创建按钮

Button通过调用接口来创建,接口调用有以下两种形式:

H
HelloCrease 已提交
11 12 13 14
- 创建包含子组件的按钮

  `Button(options?: {type?: ButtonType, stateEffect?: boolean})`,该接口用于创建包含子组件的按钮,其中type用于设置Button类型,stateEffect属性设置Button是否开启点击效果。

K
kukixi 已提交
15 16 17 18 19 20 21 22 23 24 25 26

  ```
  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)
  ```

  ![zh-cn_image_0000001260555857](figures/zh-cn_image_0000001260555857.png)

H
HelloCrease 已提交
27 28 29 30
- 创建不包含子组件的按钮

  `Button(label?: string, options?: { type?: ButtonType, stateEffect?: boolean })`,该接口用于创建不包含子组件的按钮,其中label确定所创建的Button是否包含子组件。

K
kukixi 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

  ```
  Button('Ok', { type: ButtonType.Normal, stateEffect: true })
    .borderRadius(8)
    .backgroundColor(0x317aff)
    .width(90)
  ```

  ![zh-cn_image_0000001215796030](figures/zh-cn_image_0000001215796030.png)


## 设置按钮类型

Button有三种可选类型,分别为Capsule(胶囊类型)、Circle(圆形按钮)和Normal(普通按钮),通过type进行设置。

- 胶囊按钮(默认类型)
H
HelloCrease 已提交
47

K
kukixi 已提交
48 49 50 51 52 53 54 55 56
  ```
  Button('Disable', { type: ButtonType.Capsule, stateEffect: false })
    .backgroundColor(0x317aff)
    .width(90)
  ```

  ![zh-cn_image_0000001215645452](figures/zh-cn_image_0000001215645452.png)

- 圆形按钮
H
HelloCrease 已提交
57

K
kukixi 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  ```
  Button('Circle', { type: ButtonType.Circle, stateEffect: false })
    .backgroundColor(0x317aff)
    .width(90)
    .height(90)
  ```

  ![zh-cn_image_0000001215965420](figures/zh-cn_image_0000001215965420.png)


## 自定义样式

- 设置边框弧度
  一般使用通用属性来自定义按钮样式。例如通过borderRadius属性设置按钮的边框弧度。

H
HelloCrease 已提交
73

K
kukixi 已提交
74
  ```
K
kukixi 已提交
75
  Button('circle border', { type: ButtonType.Normal })
K
kukixi 已提交
76 77 78 79 80 81 82 83
    .borderRadius(20)
  ```

  ![zh-cn_image_0000001190463780](figures/zh-cn_image_0000001190463780.png)

- 设置文本样式
  通过添加文本样式设置按钮文本的展示样式。

H
HelloCrease 已提交
84

K
kukixi 已提交
85
  ```
K
kukixi 已提交
86
  Button('font style', { type: ButtonType.Normal })
K
kukixi 已提交
87 88 89 90 91 92 93 94 95 96
    .fontSize(20)
    .fontColor(Color.Red)
    .fontWeight(800)
  ```

  ![zh-cn_image_0000001189744672](figures/zh-cn_image_0000001189744672.png)

- 设置背景颜色
  添加backgroundColor属性设置按钮的背景颜色。

H
HelloCrease 已提交
97

K
kukixi 已提交
98
  ```
K
kukixi 已提交
99
  Button('background color').backgroundColor(0xF55A42)
K
kukixi 已提交
100 101 102 103 104 105 106
  ```

  ![zh-cn_image_0000001235146483](figures/zh-cn_image_0000001235146483.png)

- 用作功能型按钮
  为删除操作创建一个按钮。

H
HelloCrease 已提交
107

K
kukixi 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  ```
  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)
  ```

  ![zh-cn_image_0000001260373911](figures/zh-cn_image_0000001260373911.png)


## 添加事件

Button组件通常用于触发某些操作,可以在绑定onClick事件来响应点击操作后的自定义行为。


```
Button('Ok', { type: ButtonType.Normal, stateEffect: true })
  .onClick(()=>{
    console.info('Button onClick')
  })
```


## 场景示例

- 用于启动操作

  可以将按钮用于启动操作的任何用户界面元素。按钮会根据用户的操作触发相应的事件。如,在List容器里边通过点击按钮进行页面跳转:

  ```
  import router from '@ohos.router'
H
HelloCrease 已提交
138

K
kukixi 已提交
139 140
  @Entry
  @Component
K
kukixi 已提交
141
  struct ButtonCase1 {
K
kukixi 已提交
142 143 144 145 146 147 148
    build() {
      List({ space: 4 }) {
        ListItem() {
          Button("First").onClick(() => {
            router.push({ url: 'xxx' })
          })
        }
H
HelloCrease 已提交
149

K
kukixi 已提交
150 151 152 153 154
        ListItem() {
          Button("Second").onClick(() => {
            router.push({ url: 'yyy' })
          })
        }
H
HelloCrease 已提交
155

K
kukixi 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        ListItem() {
          Button("Third").onClick(() => {
            router.push({ url: 'zzz' })
          })
        }
      }
      .listDirection(Axis.Vertical)
      .backgroundColor(0xDCDCDC).padding(20)
    }
  }
  ```


  ![zh-cn_image_0000001235626467](figures/zh-cn_image_0000001235626467.png)


- 用于表单的提交
  在用户登录/注册页面,用户的登录或注册的提交操作会用按钮。

H
HelloCrease 已提交
175

K
kukixi 已提交
176 177 178 179 180 181 182 183
  ```
  @Entry
  @Component
  struct ButtonCase2 {
    build() {
      Column() {
        TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
        TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
K
kukixi 已提交
184
        Button('Register').width(300).margin({ top: 20 })
K
kukixi 已提交
185 186 187 188
      }.padding(20)
    }
  }
  ```
H
HelloCrease 已提交
189

K
kukixi 已提交
190
  ![zh-cn_image_0000001190466492](figures/zh-cn_image_0000001190466492.png)