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()
相关推荐
Huang兄16 小时前
鸿蒙-深色模式适配
harmonyos·arkts·arkui
浩宇软件开发6 天前
基于DevEco鸿蒙开垃圾分类APP实现
harmonyos·arkts·arkui·垃圾分类·鸿蒙开发·deveco
钟睿6 天前
HarmonyOS花瓣地图自定义点聚合功能
android·harmonyos·arkts
ChinaDragonDreamer6 天前
HarmonyOS:知识点总结(一)
harmonyos·鸿蒙
加农炮手Jinx6 天前
Flutter for OpenHarmony 实战:JWT — 构建安全的无状态认证中心
网络·flutter·华为·harmonyos·鸿蒙
雷帝木木6 天前
Flutter for OpenHarmony:Flutter 三方库 money2 — 坚不可摧的鸿蒙金融核心组件
网络·flutter·http·华为·金融·harmonyos·鸿蒙
特立独行的猫a7 天前
uniapp-x的HarmonyOS鸿蒙应用开发:tabbar底部导航栏的实现
华为·uni-app·harmonyos·鸿蒙·uniapp-x
●VON7 天前
HarmonyOS应用开发实战(基础篇)Day10 -《鸿蒙网络请求实战》
网络·学习·华为·harmonyos·鸿蒙·von
●VON8 天前
HarmonyOS应用开发实战(基础篇)Day08-《构建布局详解上》
华为·harmonyos·鸿蒙·von
加农炮手Jinx9 天前
Flutter for OpenHarmony 实战:疯狂头像 App(三)— 复合动画与交互反馈 — 让 UI 跃动起来
flutter·ui·交互·harmonyos·鸿蒙