鸿蒙 OS 开发零基础快速入门教程

视频课程:

东西比较多, 这里主要分享一些代码和案例.

开关灯效果案例: 开灯

开关灯效果案例: 关灯

Column 和 Row 的基本用法

js 复制代码
@Entry
@Component
struct Index {
  @State message: string = '张三';

  build() {
    // 一行内容
    Row() {
      // 一列内容
      Column() {
        // 文本内容
        Text(this.message)
          .fontSize(50)// 字体大小
          .fontWeight(FontWeight.Bold) // 字体粗细
      }.width("100%")
    }.height('100%')
  }
}

一行两列的布局

js 复制代码
@Entry
@Component
struct Index {
  @State message: string = '张三';

  build() {
    // 一行内容
    Row() {
      // 一列内容
      Column() {
        // 文本内容
        Text(this.message)
          .fontSize(50)// 字体大小
          .fontWeight(FontWeight.Bold) // 字体粗细
      }.width("50%")

      // 一列内容
      Column() {
        // 文本内容
        Text(this.message)
          .fontSize(50)// 字体大小
          .fontWeight(FontWeight.Bold) // 字体粗细
      }.width("50%")
    }.height('100%')
  }
}

开关灯效果案例的基本实现

js 复制代码
@Entry
@Component
struct Index {
  @State isOn: boolean = false

  build() {
    Column({space: 10}) {
      if (this.isOn) {
        Image("pages/images/img_light.png")
          .width(300)
          .height(300)
      } else {
        Image("pages/images/img_dark.png")
          .width(300)
          .height(300)
      }


      Row({space: 30}) {
        Button("开灯").onClick(() => this.isOn = true)
        Button("关灯").onClick(() => this.isOn = false)
      }
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

图标按钮案例

js 复制代码
@Entry
@Component
struct Index {
  @State isOn: boolean = false

  build() {
    Column() {
      Button(){
        Image("pages/images/ic_delete.png")
          .width(25)
          .height(25)
      }
      .width(50)
      .height(50)
      .type(ButtonType.Circle)
      .backgroundColor(Color.Red)
      .onClick(()=>console.log("删除成功"))
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

自定义组件案例

js 复制代码
@Entry
@Component
struct Index {
  @State isOn: boolean = false

  build() {
    Column({ space: 10 }) {
      if (this.isOn) {
        Image("pages/images/img_light.png")
          .width(300)
          .height(300)
          .borderRadius(20)
      } else {
        Image("pages/images/img_dark.png")
          .width(300)
          .height(300)
          .borderRadius(20)
      }


      Row({ space: 30 }) {
        GreenButton()
          .onClick(() => this.isOn = true)

        RedButton()
          .onClick(() => this.isOn = false)
      }
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

@Component
struct RedButton {
  build() {
    Button({ type: ButtonType.Circle }) {
      Image("pages/images/icon_switch.png")
        .width(25)
        .height(25)
    }
    .width(50)
    .height(50)
    .backgroundColor(Color.Red)
  }
}


@Component
struct GreenButton {
  build() {
    Button({ type: ButtonType.Circle }) {
      Image("pages/images/icon_switch.png")
        .width(25)
        .height(25)
    }
    .width(50)
    .height(50)
    .backgroundColor(Color.Green)
  }
}

自定义组件参数案例

js 复制代码
@Entry
@Component
struct Index {
  @State isOn: boolean = false

  build() {
    Column({ space: 10 }) {
      if (this.isOn) {
        Image("pages/images/img_light.png")
          .width(300)
          .height(300)
          .borderRadius(20)
      } else {
        Image("pages/images/img_dark.png")
          .width(300)
          .height(300)
          .borderRadius(20)
      }


      Row({ space: 30 }) {
        SwitchButton({ color: Color.Green })
          .onClick(() => this.isOn = true)

        SwitchButton()
          .onClick(() => this.isOn = false)
      }
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

@Component
struct SwitchButton {
  color: Color = Color.Red

  build() {
    Button({ type: ButtonType.Circle }) {
      Image("pages/images/icon_switch.png")
        .width(25)
        .height(25)
    }
    .width(50)
    .height(50)
    .backgroundColor(this.color)
  }
}

组件文件案例

index.ets

js 复制代码
import { SwitchButton } from './SwitchButton'

@Entry
@Component
struct Index {
  @State isOn: boolean = false

  build() {
    Column({ space: 10 }) {
      if (this.isOn) {
        Image("pages/images/img_light.png")
          .width(300)
          .height(300)
          .borderRadius(20)
      } else {
        Image("pages/images/img_dark.png")
          .width(300)
          .height(300)
          .borderRadius(20)
      }


      Row({ space: 30 }) {
        SwitchButton({ color: Color.Green })
          .onClick(() => this.isOn = true)

        SwitchButton()
          .onClick(() => this.isOn = false)
      }
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

SwitchButton.ets

js 复制代码
@Component
export struct SwitchButton {
  color: Color = Color.Red

  build() {
    Button({ type: ButtonType.Circle }) {
      Image("pages/images/icon_switch.png")
        .width(25)
        .height(25)
    }
    .width(50)
    .height(50)
    .backgroundColor(this.color)
  }
}
相关推荐
志存高远662 小时前
(面试)Android各版本新特性
android
繁依Fanyi2 小时前
我的 PDF 工具箱:CodeBuddy 打造 PDFMagician 的全过程记录
java·pdf·uni-app·生活·harmonyos·codebuddy首席试玩官
IT从业者张某某2 小时前
信奥赛-刷题笔记-队列篇-T3-P3662Why Did the Cow Cross the Road II S
android·笔记
Huang兄2 小时前
#跟着若城学鸿蒙# web篇-获取定位
harmonyos
未来之窗软件服务2 小时前
Cacti 未经身份验证SQL注入漏洞
android·数据库·sql·服务器安全
BXCQ_xuan2 小时前
handsome主题美化及优化:10.1.0最新版 - 2
android
圈圈编码3 小时前
MVVM框架
android·学习·kotlin
zkmall4 小时前
Java + 鸿蒙双引擎:ZKmall开源商城如何定义下一代B2C商城技术标准?
java·开源·harmonyos
橙子199110165 小时前
在 Kotlin 中,什么是解构,如何使用?
android·开发语言·kotlin
lqj_本人5 小时前
鸿蒙OS&UniApp实现视频播放与流畅加载:打造完美的移动端视频体验#三方框架 #Uniapp
uni-app·音视频·harmonyos