HarmonyOS自学-Day4(TodoList案例)

目录


文章声明⭐⭐⭐

  1. 该文章为我(有编程语言基础,非编程小白)的 HarmonyOS自学笔记,此类文章笔记我会默认大家都学过前端相关的知识
  2. 知识来源为 HarmonyOS官方文档,归纳为自己的语言与理解记录于此
  3. 不出意外的话,我大抵会 持续更新
  4. 想要了解前端开发(技术栈大致有:Vue2/3、微信小程序、uniapp、HarmonyOS、NodeJS、Typescript)与Python的小伙伴,可以关注我!谢谢大家!

让我们开始今天的学习吧!

TodoList小案例

效果图如下:

代码如下:

typescript 复制代码
@Entry
@Component
struct Index {
  @State userInput:string = ''; // 用户输入
  @State toDoList:string[] = ['吃饭','睡觉','打豆豆']; // 待办事项的数组
  build() {
    Column(){
      // 标题
      Row(){
        Text('TodoList')
          .fontSize(40)
      }
      .margin({top:100})
      // 输入框与提交按钮
      Row(){
        // 输入框
        TextInput({text:this.userInput,placeholder:'请输入代办项:'})
          .width(220)
          .height(50)
          .fontSize(20)
          .onChange((newValue:string)=>{this.userInput = newValue})

        // 提交按钮
        Button('提交')
          .width(80)
          .height(50)
          .onClick(()=>{
            // 提交至待办事项数组
            this.toDoList.push(this.userInput)
            // 清空用户输入
            this.userInput = ''
          })
      }
      .width('100%')
      .height('10%')
      .margin({top:30})
      .backgroundColor(Color.White)
      .justifyContent(FlexAlign.SpaceAround)
      // 待办事项列表
      List(){
        if (this.toDoList.length) {
          // 展示列表
          ForEach(this.toDoList,(item:string,index:number)=>{
            ListItem(){
              Row(){
                // 代办事项内容
                Text(item)
                  .fontSize(25)
                // 删除按钮
                Button('删除')
                  .width(100)
                  .backgroundColor(Color.Red)
                  .fontColor(Color.White)
                  .onClick(()=>{
                    this.toDoList.splice(index,1)
                  })
              }
              .width('100%')
              .justifyContent(FlexAlign.SpaceBetween)
            }
            .width('80%')
            .height(50)
            .margin({top:10})
          },(item:string,index:number)=>item+index.toString())
        } else {
          // 数组为空,展示提示文字
          ListItem(){
            Text('暂无待办事项')
              .fontSize(30)
          }
          .margin({top:30})
        }
      }
      .width('100%')
      .height('50%')
      .alignListItem(ListItemAlign.Center)
      .scrollBar(BarState.Auto)
    }
    .width('100%')
    .height('100%')
  }
}
相关推荐
Davina_yu33 分钟前
弹窗交互:AlertDialog与CustomDialog的创建与关闭(11)
harmonyos·鸿蒙·鸿蒙系统
90后的晨仔1 小时前
HarmonyOS 锁屏音频播放完整实践指南
harmonyos
90后的晨仔1 小时前
鸿蒙应用动态桌面图标功能实现完全指南
harmonyos
nashane1 小时前
HarmonyOS 6学习:JsCrash“闪退”法医指南——从FaultLog堆栈还原崩溃现场的终极手册
学习·华为·harmonyos
李二。3 小时前
鸿蒙OS NEXT 批量重命名工具:PC端文件管理的效率革命
华为·harmonyos
HwJack203 小时前
鸿蒙背景下 Cocos Creator 的三大 JS 引擎:JIT 与热更新的十字路口
javascript·华为·harmonyos
提子拌饭1333 小时前
Column 嵌套布局:多级 Column 实现复杂纵向结构——鸿蒙 HarmonyOS ArkTS 原生学习应用
学习·华为·harmonyos·鸿蒙·鸿蒙系统
前端不太难5 小时前
鸿蒙 App 分布式数据同步:架构设计 + Demo 实现
分布式·状态模式·harmonyos
酣大智6 小时前
BGP选路原则--下一跳IGP Metric小的(8)
网络·华为·路由·bgp