第九节HarmonyOS 常用基础组件4-Button

一、Button

Button组件主要用来响应点击操作,可以包含子组件。

示例代码:

复制代码
@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        Button('确定', { type: ButtonType.Capsule, stateEffect: true })
          .width('90%')
          .height(40)
          .fontSize(16)
          .fontWeight(FontWeight.Medium)
          .backgroundColor('#007DFF')

      }
      .width('100%')
    }
    .height('100%')
  }
}

效果图:

二、设置按钮样式

type用于定义按钮样式,示例代码中ButtonType.Capsule表示胶囊形按钮;stateEffect用于设置按钮按下时是否开启切换效果,当状态置为false时,点击效果关闭,默认值为true。

我们可以设置多种样式的Button,除了Capsule可以以设置Normal和Circle:

代码:

复制代码
@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        Button('确定', { type: ButtonType.Normal, stateEffect: true })
          .width('90%')
          .height(50)
          .fontSize(20)
          .backgroundColor('#007DFF')
        Blank(10)
        Button('确定1', { type: ButtonType.Capsule, stateEffect: true })
          .width('90%')
          .height(50)
          .fontSize(20)
          .backgroundColor('#007DFF')
        Blank(10)
        Button('确定', { type: ButtonType.Circle, stateEffect: true })
          .width('90%')
          .height(100)
          .fontSize(20)
          .backgroundColor('#007DFF')

      }
      .width('100%')
    }
    .height('100%')
  }
}
  1. Capsule:胶囊型按钮(圆角默认为高度的一半)。
  1. Circle:圆形按钮。
  1. Normal:普通按钮(默认不带圆角)。

三、设置按钮点击事件

可以给Button绑定onClick事件,每当用户点击Button的时候,就会回调执行onClick方法,调用里面的逻辑代码。

复制代码
Button('登录', { type: ButtonType.Capsule, stateEffect: true })
  ...
  .onClick(() => {
  // 处理点击事件逻辑
  })

四、包含子组件

Button组件可以包含子组件,让您可以开发出更丰富多样的Button,下面的示例代码中Button组件包含了一个Image组件:

复制代码
Button({ type: ButtonType.Circle, stateEffect: true }) {
  Image($r('app.media.icon_delete'))
    .width(30)
    .height(30)
}
.width(55)
.height(55)
.backgroundColor(0x317aff)

效果图如下:

相关推荐
颜颜yan_36 分钟前
【HarmonyOS5】UIAbility组件生命周期详解:从创建到销毁的全景解析
架构·harmonyos·鸿蒙·鸿蒙系统
龙儿筝4 小时前
ArkUI-X与Android桥接通信之消息通信
harmonyos
陈奕昆5 小时前
5.1 HarmonyOS NEXT系统级性能调优:内核调度、I/O优化与多线程管理实战
华为·harmonyos
androidstarjack5 小时前
如何评价华为最新长焦专利技术?能否颠覆手机长焦摄影的目前限制?
华为·智能手机
libo_20256 小时前
HarmonyOS5 全球化运营:使用AGC的本地化工具适配30+国家/地区
harmonyos
程序员小刘7 小时前
如何优化React Native应用以适配HarmonyOS5?
javascript·react native·react.js·华为·harmonyos
anyup7 小时前
快崩溃了!华为应用商店已经 4 次驳回我的应用上线
前端·华为·uni-app
程序员小刘7 小时前
【HarmonyOS 5】拍摄美化开发实践介绍以及详细案例
华为·harmonyos
晓北斗NorSnow8 小时前
华为手机开机卡在Huawei界面不动怎么办?
华为·智能手机
王二蛋与他的张大花8 小时前
HarmonyOS运动开发:打造你的专属运动节拍器
harmonyos