第九节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)

效果图如下:

相关推荐
再见6585 小时前
【HarmonyOS】 Todo 应用开发实战
harmonyos
爱吃大芒果6 小时前
面向大型鸿蒙原生应用的工程基建:核心路由、全局样式库与状态管理设计图纸
华为·harmonyos
轻口味10 小时前
HarmonyOS 6.1.1 全栈实战录 - 91 实战 Call Service Kit 扩展企服来去电智慧
华为·harmonyos·鸿蒙
木斯佳11 小时前
鸿蒙开发入门指南:前端开发者快速理解视频编码概念——输入模式
华为·音视频·harmonyos
不羁的木木13 小时前
《HarmonyOS技术精讲》二:用户动作与状态感知实战
华为·harmonyos
G_dou_16 小时前
Flutter+OpenHarmony 实战:stopwatch 秒表应用
flutter·harmonyos
亚信安全官方账号16 小时前
AISTrustOne鸿蒙版安全方案 让终端防护“内生”力量觉醒
安全·华为·harmonyos
夜勤月16 小时前
HarmonyOS 6.0 ArkWeb实战:PDF背景色自定义功能全解析(附完整代码+避坑指南)
华为·pdf·harmonyos
想你依然心痛17 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“药界智脑“——PC端AI智能体沉浸式药物研发与分子模拟工作台
人工智能·华为·ar·harmonyos·智能体
G_dou_18 小时前
Flutter +OpenHarmony 实战:clock 时钟应用
flutter·harmonyos