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%')
  }
}
相关推荐
贾伟康3 小时前
【HarmonyOS 7新能力|020】LazyLayoutAlgorithm入门实战:从能力边界到最小可运行链路
harmonyos·arkts·arkui·harmonyos 7·lazylayout
梦想不只是梦与想4 小时前
鸿蒙 邀请测试:发布测试版本
harmonyos·appgallery 邀请测试·邀请测试
Winner_hwx5 小时前
华为ICT大赛备赛复习1
服务器·网络·华为
马剑威(威哥爱编程)6 小时前
【共创稿事节】HarmonyOS 7 应用 Skill 化实战:从“被打开“到“被调用“,把功能递进系统意图分发池
pytorch·深度学习·harmonyos
HarmonyOS_SDK8 小时前
HarmonyOS Push Kit 自分类权益 Skill,助力提升权益申请通过率
harmonyos
周胡杰10 小时前
将现有 Compose Multiplatform 业务接入 HarmonyOS:架构、适配与持续同步
harmonyos·鸿蒙·cmp
星栖与芯11 小时前
LiteOS-M 切换汇编逐行图解(1):汇编是什么·寄存器与栈
汇编·stm32·嵌入式硬件·harmonyos·鸿蒙系统
HwJack2011 小时前
【HarmonyOS开发小实践】ArkUI 应用级状态AppStorage 与跨页面共享、持久化
ui·华为·性能优化·harmonyos
昇腾知识体系13 小时前
vLLM-Ascend 支持矩阵:supported_models 模型列表、BF16/ACLGraph 支持项核对方法
人工智能·华为·知识图谱·vllm
万物智能信息科技14 小时前
板载按键key的ADC转换和信号控制—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
嵌入式硬件·华为·开源·harmonyos·鸿蒙