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

【Harmony OS 4.0】待办列表案例

在这里插入图片描述
src/main/ets/example1/Models.ets

// 定义class类数据模型
export class TaskDataModel {// private 私有属性,在类对象外不允许随意更改数据,必须本地初始化。private tasks: Array<string> = ['早起晨练', '准备早餐', '阅读名著', '学习ArkTs', '玩游戏放松']getData(): Array<string> {return this.tasks}
}

src/main/ets/example1/ItemComponent.ets

// 子组件
@Component
export default struct ItemComponent {@Prop task_content: string // 从父组件接收待办任务的内容@State isComplete: boolean = false // 定义状态变量isComplete,是否完成该任务的标记@Builder // @Builder 组件内自定义构建函数,按值传递参数CreateIcon(icon: Resource): void { // icon: Resource 表示资源引用类型Column() {Image(icon).width(28).height(28).objectFit(ImageFit.Contain).margin(10)}}build() {Row() {Column() {if (this.isComplete) {this.CreateIcon($r('app.media.ic_ok3'))} else {this.CreateIcon($r('app.media.ic_default'))}}Column() {Text(this.task_content).width('100%').fontSize(20).fontWeight(500).decoration({type: this.isComplete ? TextDecorationType.LineThrough : TextDecorationType.None,color: Color.Black})// 根据任务是否完成确定文本删除线.opacity(this.isComplete ? .6 : 1) // 根据任务是否完成来设置文本透明度}.onClick(() => {this.isComplete = !this.isComplete})}.margin({ bottom: 8, left: 16, right: 16 }).borderRadius(20).padding(10).backgroundColor('#FFFFFF')}
}

src/main/ets/example1/index.ets

// 父组件
import { TaskDataModel } from './Models'
import ItemComponent from "./ItemComponent"@Entry
@Component
struct TaskListIndex {private totalDate: Array<string> = []// 自定义组件生命周期: 组件即将出现时回调该接口,在创建自定义组件新实例后,在执行其build()函数之前执行。aboutToAppear(): void {this.totalDate = new TaskDataModel().getData() // 初始化数据}build() { // 构建入口的UI界面Column({ space: 3 }) {Text('待办').fontSize(30).fontWeight(800).width('90%').height(50).textAlign(TextAlign.Start)List() {ForEach(this.totalDate, (item: string) => {ItemComponent({ task_content: item })})}}.width('100%').height('100%').backgroundColor('#EEEEEE')}
}
http://www.lryc.cn/news/426792.html

相关文章:

  • 快速把文件名统计到excel表的方法
  • 开源通用验证码识别OCR —— DdddOcr 源码赏析(一)
  • 上升ECMAScript性能优化技巧与陷阱(下)
  • 用7EPhone云手机进行TikTok的矩阵运营
  • 谷歌浏览器下载文件被阻止怎么解除
  • apt E: 无法定位软件包 winehq-stable
  • P2460[SDOI2007] 科比的比赛
  • linux学习--第二天
  • 使用 Flask、Celery 和 Python 实现每月定时任务
  • 【c语言】整数在内存中的储存(大小端字节序)
  • 浅谈SIMD、向量化处理及其在StarRocks中的应用
  • 【ML】Image Augmentation)的作用、使用方法及其分类
  • 设计模式六大原则(一)--单一职责原则
  • c语言学习,malloc()函数分析
  • 【运维项目经历|041】上云项目-物理机迁移到阿里云
  • 分组并合并其它列的非空值 --Excel难题#83
  • VM相关配置及docker
  • Redis中Set数据类型常用命令
  • mysql误删数据恢复记录
  • 论文阅读:Real-time Controllable Denoising for Image and Video
  • 【Kubernetes】虚拟 IP 与 Service 的代理模式
  • 深度学习·Pytorch
  • fastzdp_sqlmodel新增get_first和is_exitsts方法
  • 嵌入式软件--数电基础 DAY 3
  • 【生成式人工智能-十五-经典的影像生成方法-GAN】
  • python 已知x+y=8 求x*y*(x-y)的最大值
  • windows平台的postgresql主从数据库流备份
  • Spring 常见设计模式
  • 优化大量数据导出到Excel的内存消耗(二):如果数据超出Excel单表上限,则进行分表
  • rustrover打开会报Error: Invalid toolchain