HarmonyOS学习4 --- 创建一个页面

1、声明式UI语法

复制代码
@Entry
@Component
struct My_page {
  @State isLogin: boolean = false

  build() {
    Row() {
      Image(this.isLogin ? $r('app.media.icon_leon') : $r('app.media.icon'))
        .height(60)
        .width(60)
        .onClick(() => {
          this.isLogin = !this.isLogin
        })

      Text(this.isLogin ? $r('app.string.string_to_unlogin') : $r('app.string.string_to_unlogin'))
        .fontWeight(FontWeight.Bold)
        .fontSize(20)
        .fontColor(this.isLogin ? Color.Orange : Color.Black)
        .margin({ "top": "0.00vp", "right": "0.00vp", "bottom": "0.00vp", "left": "10.00vp" })
    }
    .width('100%')
    .margin({ "top": "20.00vp", "right": "10.00vp", "bottom": "0.00vp", "left": "10.00vp" })
  }
}

2、自定义组件

2.1、自定义组件生命周期

注:页面,即由 @Entry 修饰的自定义组件

注:自定义组件,即由 @Component 修饰的UI单元

1)aboutToAppear()

2)onPageShow()

注:仅在被 @Entry 修饰的组件中生效

3)onBackPress()

注:仅在被 @Entry 修饰的组件中生效

4)onPageHide()

注:仅在被 @Entry 修饰的组件中生效

5)aboutToDisappear()

2.2、案例

注:@State 可建立起视图与状态之间的绑定关系

  • ToItem

    @Component
    export default struct ToItem {
    public message?: string
    @State isComplete: boolean = false

    复制代码
    @Builder leonSelectLabel(icon: Resource) {
      Image(icon)
        .objectFit(ImageFit.Contain)
        .width('28vp')
        .width('28vp')
        .margin('20vp')
    
    }
    
    build() {
      Row() {
        if (this.isComplete) {
          this.leonSelectLabel($r('app.media.ic_ok'))
        } else {
          this.leonSelectLabel($r('app.media.ic_default'))
        }
        Text(this.message??'无')
          .fontSize('20fp')
          .fontWeight(FontWeight.Bold)
          .decoration({ type: this.isComplete ? TextDecorationType.LineThrough : TextDecorationType.None })
          .fontColor(this.isComplete ? Color.Gray : Color.Black)
      }
      .width('100%')
      .borderRadius(6)
      .backgroundColor(Color.White)
      .onClick(() => {
        this.isComplete = !this.isComplete
      })
    }

    }

复制代码
ToDoListPage
复制代码
import ToDoItem from '../view/ToDoItem'
import DataModel from '../viewmodel/DataModel'

@Entry
@Component
struct ToDoListPage {
  private todoLists: string[]

  aboutToAppear() {
    this.todoLists = DataModel.getData()
  }

  build() {

    Column({ space: 16 }) {

      Text("待办任务列表")
        .fontWeight(FontWeight.Bold)
        .fontSize(30)
        .fontColor(Color.Blue)
        .margin({ top: 10, left: 10 })


      ForEach(this.todoLists, (item: string) => {
        ToDoItem({ message: item }).width('90%')
      }, (item: string) => JSON.stringify(item))

    }
    .width('100%')
    .height('100%')
    .backgroundColor($r('app.color.page_background'))

  }
}
复制代码
DataModel
复制代码
export class DataModel {
  public tasks: string[] = ["早起晨练", "准备早饭", "学习编程", "阅读名著", "自由活动"]

  getData() {
    return this.tasks
  }
}


export default new DataModel()
相关推荐
坚果派·白晓明1 天前
三方库ada
harmonyos·鸿蒙·openharmony
云和数据.ChenGuang2 天前
鸿蒙餐饮系统:全场景智慧餐饮新范式
人工智能·机器学习·华为·数据挖掘·harmonyos·鸿蒙·鸿蒙系统
云和数据.ChenGuang3 天前
鸿蒙智联,极智共生:HarmonyOS与MiniMax智能体的融合新纪元
华为·harmonyos·鸿蒙
UnicornDev3 天前
【HarmonyOS 6】今日统计卡片实战:运动记录数据概览
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
ShuiShenHuoLe3 天前
组件的状态ComponentV2
harmonyos·鸿蒙
仓颉编程语言4 天前
CangjieSkills 正式开源:为仓颉 AI 编程打造的“技能增强“方案,实测降低 60% 费用
华为·ai编程·鸿蒙·仓颉编程语言
左手厨刀右手茼蒿5 天前
Flutter 三方库 all_lint_rules_community 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、严谨、基于全量社区 Lint 规则的工业级静态代码质量与安全审计引擎
flutter·harmonyos·鸿蒙·openharmony·all_lint_rules_community
雷帝木木5 天前
Flutter for OpenHarmony:Flutter 三方库 cbor 构建 IoT 设备的极致压缩防窃协议(基于标准二进制 JSON 表达格式)
网络·物联网·flutter·http·json·harmonyos·鸿蒙
王码码20355 天前
Flutter 三方库 servicestack 的鸿蒙化适配指南 - 实现企业级 Message-based 架构集成、支持强类型 JSON 序列化与跨端服务调用同步
flutter·harmonyos·鸿蒙·openharmony·message-based
里欧跑得慢5 天前
Flutter 三方库 jsonata_dart 的鸿蒙化适配指南 - 实现高性能的 JSON 数据查询与转换、支持 JSONata 表达式引擎与端侧复杂数据清洗
flutter·harmonyos·鸿蒙·openharmony·jsonata_dart