IT评测·应用市场-qidao123.com

标题: 鸿蒙HarmonyOS实战-ArkUI组件(Button) [打印本页]

作者: 圆咕噜咕噜    时间: 2024-6-8 09:37
标题: 鸿蒙HarmonyOS实战-ArkUI组件(Button)
 一、Button

Button(按钮)是一种常见的用户界面控件,通常用于触发操作或提交数据。Button 拥有文本标签和一个可点击的地区,用户点击该地区即可触发相应的操作或事件。
Button 的主要功能有:
1.创建按钮

语法阐明:
  1. Button(label?: string, options?: { type?: ButtonType, stateEffect?: boolean })
  2. Button(options?: {type?: ButtonType, stateEffect?: boolean})
复制代码
使用:
  1. @Entry
  2. @Component
  3. struct Index {
  4.   build() {
  5.     Column(){
  6.       Button('Ok', { type: ButtonType.Normal, stateEffect: true })
  7.         .borderRadius(8)
  8.         .backgroundColor(0x317aff)
  9.         .width(90)
  10.         .height(40)
  11.       Button({ type: ButtonType.Normal, stateEffect: true }) {
  12.         Row() {
  13.           Image($r('app.media.app_icon')).width(20).height(40).margin({ left: 12 })
  14.           Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
  15.         }.alignItems(VerticalAlign.Center)
  16.       }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)
  17.     }
  18.   }
  19. }
复制代码

2.设置按钮范例

  1. @Entry
  2. @Component
  3. struct Index {
  4.   build() {
  5.     Column(){
  6.       Button('Disable', { type: ButtonType.Capsule, stateEffect: false })
  7.         .backgroundColor(0x317aff)
  8.         .width(90)
  9.         .height(40)
  10.       Button('Circle', { type: ButtonType.Circle, stateEffect: false })
  11.         .backgroundColor(0x317aff)
  12.         .width(90)
  13.         .height(90)
  14.       Button('Circle', { type: ButtonType.Normal, stateEffect: false })
  15.         .backgroundColor(0x317aff)
  16.         .width(90)
  17.         .height(90)
  18.     }
  19.   }
  20. }
复制代码

留意:不支持通过borderRadius属性重新设置
3.自定义样式

  1. @Entry
  2. @Component
  3. struct Index {
  4.   build() {
  5.     Column(){
  6.       Button('circle border', { type: ButtonType.Normal })
  7.         .borderRadius(20)
  8.         .height(40)
  9.       Button('font style', { type: ButtonType.Normal })
  10.         .fontSize(20)
  11.         .fontColor(Color.Pink)
  12.         .fontWeight(800)
  13.       Button('background color').backgroundColor(0xF55A42)
  14.       Button({ type: ButtonType.Circle, stateEffect: true }) {
  15.         Image($r('app.media.ic_public_refresh')).width(30).height(30)
  16.       }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
  17.     }
  18.   }
  19. }
复制代码

4.添加事件

  1. Button('Ok', { type: ButtonType.Normal, stateEffect: true })
  2.   .onClick(()=>{
  3.     console.info('Button onClick')
  4.   })
复制代码
5.案例

Button按钮的实际应用场景主要包罗以下几个方面:
Button按钮的应用场景非常广泛,基本上所有必要用户交互的场景都可以使用Button按钮来实现。





欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4