当前位置: 首页 > news >正文

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

1、声明式UI语法

 

@Entry
@Component
struct My_page {@State isLogin: boolean = falsebuild() {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()

http://www.dtcms.com/a/268053.html

相关文章:

  • 多模态偏好数据集生成与混合偏好优化(MPO)方法
  • 计算机网络1.1:什么是Internet?
  • 自定义指令
  • 一条 SQL 语句的内部执行流程详解(MySQL为例)
  • 进程控制中URL攻击与修复方法
  • ether0 大语言推理模型生成SMILES 的分子
  • java并发编程--可见性、原子性、有序性
  • 进程终止:exit()与_exit()深度解析
  • 模块化汽车基础设施的正面交锋---区域架构与域架构
  • 电信、移动、联通、广电跨运营商网速慢原因
  • QML与C++交互之QML端信号绑定C++端槽函数
  • uniapp实现的多种时间线模板
  • jmm,`as - if - serial` 与 `happens - before` 原则
  • Dubbo 3.x源码(31)—Dubbo消息的编码解码
  • 容声W60以光水离子科技实现食材“主动养鲜”
  • 创客匠人深度剖析:家庭教育赛道创始人 IP 打造与知识变现的破局之道
  • 【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
  • 0704-0706上海,又聚上了
  • 【MyBatis】实现数据库的增、删、改、查
  • 深度解析命令模式:将请求封装为对象的设计智慧
  • 儿童趣味记忆配对游戏
  • LeetCode 75. 颜色分类(荷兰国旗问题)
  • 一次佳能iX6780彩色喷墨打印机报5B00维修的记录
  • 【网络协议安全】任务13:ACL访问控制列表
  • 牛客周赛Round 99(Go语言)
  • 《kubernetes》k8s实战之部署PHP/JAVA网站
  • 中级统计师-经济学基础知识-第四章 国民收入核算
  • 单片机物联网应用中的 Pogopin、串口与外围模组通信技术解析
  • Java 大视界 -- Java 大数据在智能教育在线课程学习效果影响因素分析与优化设计(334)
  • Zotero中进行文献翻译【Windows11】