1# 按钮 (Button) 2 3 4Button是按钮组件,通常用于响应用户的点击操作,其类型包括胶囊按钮、圆形按钮、普通按钮。Button做为容器使用时可以通过添加子组件实现包含文字、图片等元素的按钮。具体用法请参考[Button](../reference/apis-arkui/arkui-ts/ts-basic-components-button.md)。 5 6 7## 创建按钮 8 9Button通过调用接口来创建,接口调用有以下两种形式: 10 11 12- 创建不包含子组件的按钮。 13 14 ```ts 15 Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean }) 16 ``` 17 18 其中,label用来设置按钮文字,type用于设置Button类型,stateEffect属性设置Button是否开启点击效果。 19 20 ```ts 21 Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 22 .borderRadius(8) 23 .backgroundColor(0x317aff) 24 .width(90) 25 .height(40) 26 ``` 27 28  29 30 31- 创建包含子组件的按钮。 32 33 ```ts 34 Button(options?: {type?: ButtonType, stateEffect?: boolean}) 35 ``` 36 37 只支持包含一个子组件,子组件可以是[基础组件](../reference/apis-arkui/arkui-ts/ts-basic-components-blank.md)或者[容器组件](../reference/apis-arkui/arkui-ts/ts-container-badge.md)。 38 39 ```ts 40 Button({ type: ButtonType.Normal, stateEffect: true }) { 41 Row() { 42 Image($r('app.media.loading')).width(20).height(40).margin({ left: 12 }) 43 Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 }) 44 }.alignItems(VerticalAlign.Center) 45 }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40) 46 ``` 47 48  49 50 51## 设置按钮类型 52 53Button有三种可选类型,分别为胶囊类型(Capsule)、圆形按钮(Circle)和普通按钮(Normal),通过type进行设置。 54 55 56- 胶囊按钮(默认类型) 57 58 此类型按钮的圆角自动设置为高度的一半,不支持通过borderRadius属性重新设置圆角。 59 60 ```ts 61 Button('Disable', { type: ButtonType.Capsule, stateEffect: false }) 62 .backgroundColor(0x317aff) 63 .width(90) 64 .height(40) 65 ``` 66 67  68 69 70- 圆形按钮 71 72 此类型按钮为圆形,不支持通过borderRadius属性重新设置圆角。 73 74 ```ts 75 Button('Circle', { type: ButtonType.Circle, stateEffect: false }) 76 .backgroundColor(0x317aff) 77 .width(90) 78 .height(90) 79 ``` 80 81  82 83- 普通按钮 84 85 此类型的按钮默认圆角为0,支持通过borderRadius属性重新设置圆角。 86 87 ```ts 88 Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 89 .borderRadius(8) 90 .backgroundColor(0x317aff) 91 .width(90) 92 .height(40) 93 ``` 94 95  96 97 98## 自定义样式 99 100- 设置边框弧度。 101 102 使用通用属性来自定义按钮样式。例如通过borderRadius属性设置按钮的边框弧度。 103 104 ```ts 105 Button('circle border', { type: ButtonType.Normal }) 106 .borderRadius(20) 107 .height(40) 108 ``` 109 110  111 112 113- 设置文本样式。 114 115 通过添加文本样式设置按钮文本的展示样式。 116 117 ```ts 118 Button('font style', { type: ButtonType.Normal }) 119 .fontSize(20) 120 .fontColor(Color.Pink) 121 .fontWeight(800) 122 ``` 123 124  125 126 127- 设置背景颜色。 128 129 添加backgroundColor属性设置按钮的背景颜色。 130 131 ```ts 132 Button('background color').backgroundColor(0xF55A42) 133 ``` 134 135  136 137 138- 创建功能型按钮。 139 140 为删除操作创建一个按钮。 141 142 ```ts 143 let MarLeft: Record<string, number> = { 'left': 20 } 144 Button({ type: ButtonType.Circle, stateEffect: true }) { 145 Image($r('app.media.ic_public_delete_filled')).width(30).height(30) 146 }.width(55).height(55).margin(MarLeft).backgroundColor(0xF55A42) 147 ``` 148 149  150 151 152## 添加事件 153 154Button组件通常用于触发某些操作,可以绑定onClick事件来响应点击操作后的自定义行为。 155 156```ts 157Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 158 .onClick(()=>{ 159 console.info('Button onClick') 160 }) 161``` 162 163 164## 场景示例 165 166- 用于启动操作。 167 168 可以用按钮启动任何用户界面元素,按钮会根据用户的操作触发相应的事件。例如,在List容器里通过点击按钮进行页面跳转。 169 170 ```ts 171 // xxx.ets 172 import router from '@ohos.router'; 173 @Entry 174 @Component 175 struct ButtonCase1 { 176 @State FurL:router.RouterOptions = {'url':'pages/first_page'} 177 @State SurL:router.RouterOptions = {'url':'pages/second_page'} 178 @State TurL:router.RouterOptions = {'url':'pages/third_page'} 179 build() { 180 List({ space: 4 }) { 181 ListItem() { 182 Button("First").onClick(() => { 183 router.pushUrl(this.FurL) 184 }) 185 .width('100%') 186 } 187 ListItem() { 188 Button("Second").onClick(() => { 189 router.pushUrl(this.SurL) 190 }) 191 .width('100%') 192 } 193 ListItem() { 194 Button("Third").onClick(() => { 195 router.pushUrl(this.TurL) 196 }) 197 .width('100%') 198 } 199 } 200 .listDirection(Axis.Vertical) 201 .backgroundColor(0xDCDCDC).padding(20) 202 } 203 } 204 ``` 205 206  207 208 209- 用于提交表单。 210 211 在用户登录/注册页面,使用按钮进行登录或注册操作。 212 213 ```ts 214 // xxx.ets 215 @Entry 216 @Component 217 struct ButtonCase2 { 218 build() { 219 Column() { 220 TextInput({ placeholder: 'input your username' }).margin({ top: 20 }) 221 TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 }) 222 Button('Register').width(300).margin({ top: 20 }) 223 .onClick(() => { 224 // 需要执行的操作 225 }) 226 }.padding(20) 227 } 228 } 229 ``` 230 231  232 233- 悬浮按钮 234 235 在可以滑动的界面,滑动时按钮始终保持悬浮状态。 236 237 ```ts 238 // xxx.ets 239 @Entry 240 @Component 241 struct HoverButtonExample { 242 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 243 build() { 244 Stack() { 245 List({ space: 20, initialIndex: 0 }) { 246 ForEach(this.arr, (item:number) => { 247 ListItem() { 248 Text('' + item) 249 .width('100%').height(100).fontSize(16) 250 .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF) 251 } 252 }, (item:number) => item.toString()) 253 }.width('90%') 254 Button() { 255 Image($r('app.media.ic_public_add')) 256 .width(50) 257 .height(50) 258 } 259 .width(60) 260 .height(60) 261 .position({x: '80%', y: 600}) 262 .shadow({radius: 10}) 263 .onClick(() => { 264 // 需要执行的操作 265 }) 266 } 267 .width('100%') 268 .height('100%') 269 .backgroundColor(0xDCDCDC) 270 .padding({ top: 5 }) 271 } 272 } 273 ``` 274 275  276