HarmonyOS自学-Day4(TodoList案例)


文章声明⭐⭐⭐

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

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

TodoList小案例

效果图如下:
在这里插入图片描述
代码如下:

@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%')
  }
}

继续加功能

@Entry
@Component
struct Index {
  @State userInput:string = ''; // 用户输入
  @State toDoList:string[] = []; // 待办事项的数组
  build() {
    Column(){
      // 标题
      Row(){
        Text('TodoList')
          .fontSize(40)
      }
      .margin({top:100})
      // 全部完成
      Row(){
        Row(){
          CheckboxGroup({group:'listGroup'})
            .margin({right:10})
          Text('全部完成')
            .fontSize(20)
        }
      }
      .width('90%')
      .margin({top:30})
      .justifyContent(FlexAlign.SpaceBetween)
      // 输入框与提交按钮
      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:10})
      .backgroundColor(Color.White)
      .justifyContent(FlexAlign.SpaceAround)
      // 待办事项列表
      List(){
        if (this.toDoList.length) {
          // 展示列表
          ForEach(this.toDoList,(item:string,index:number)=>{
            ListItem(){
              toDo({item,index,callback:this.deleteToDo})
            }
            .width('90%')
            .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%')
  }
  // 删除待办事项
  deleteToDo = (index:number)=>{
    this.toDoList.splice(index,1)
  }
}

@Component
struct toDo {
  private item:string
  private index:number
  private callback:(index:number)=>void
  @State isChecked:boolean = false
  build(){
    Row(){
      // 勾选框
      Checkbox({group:'listGroup'})
        .select(this.isChecked)
        .onChange((value:boolean)=>{
          this.isChecked = value
        })
      // 代办事项内容
      Text(this.item)
        .fontSize(25)
        .decoration({type:this.isChecked?TextDecorationType.LineThrough:TextDecorationType.None})
      // 删除按钮
      Button('删除')
        .width(80)
        .backgroundColor(Color.Red)
        .fontColor(Color.White)
        .onClick(()=>{
          this.callback(this.index)
        })
    }
    .width('100%')
    .justifyContent(FlexAlign.SpaceBetween)
  }
}

效果如下:
在这里插入图片描述

  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,TodoList是一种常见的待办事项管理工具,可以帮助人们记录和管理日常生活和工作中需要完成的任务。以下是一个简单的TodoList案例,您可以了解一下: 假设您正在开发一个简单的TodoList应用。该应用需要实现以下功能: 1. 用户可以添加新的待办事项,包括任务名称、截止日期和优先级。 2. 用户可以查看所有待办事项的列表,并根据截止日期和优先级进行排序。 3. 用户可以编辑和删除待办事项。 4. 用户可以标记已完成的任务。 下面是一个使用Python和Flask框架实现上述功能的简单示例代码: ```python from flask import Flask, render_template, request, redirect, url_for import datetime app = Flask(__name__) # 初始任务列表 tasks = [ { 'name': '购买牛奶', 'due_date': datetime.date(2021, 9, 30), 'priority': '高', 'completed': False }, { 'name': '发送邮件', 'due_date': datetime.date(2021, 10, 1), 'priority': '中', 'completed': False } ] # 首页 @app.route('/') def index(): return render_template('index.html', tasks=tasks) # 添加新任务 @app.route('/add_task', methods=['POST']) def add_task(): name = request.form['name'] due_date = datetime.datetime.strptime(request.form['due_date'], '%Y-%m-%d').date() priority = request.form['priority'] task = { 'name': name, 'due_date': due_date, 'priority': priority, 'completed': False } tasks.append(task) return redirect(url_for('index')) # 删除任务 @app.route('/delete_task/<int:task_index>') def delete_task(task_index): tasks.pop(task_index) return redirect(url_for('index')) # 标记任务完成 @app.route('/complete_task/<int:task_index>') def complete_task(task_index): tasks[task_index]['completed'] = True return redirect(url_for('index')) if __name__ == '__main__': app.run() ``` 在上述示例代码中,我们使用了Python的datetime模块来处理日期,使用了Flask框架来实现Web应用程序的相关功能。当用户添加、删除或标记已完成的任务时,使用了重定向来返回到主页。当然,这只是一个简单的TodoList案例,您可以根据自己的需求对其进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斯丢匹德先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值