「Mac畅玩鸿蒙与硬件32」UI互动应用篇9 - 番茄钟倒计时应用 ...

打印 上一主题 下一主题

主题 1800|帖子 1800|积分 5404

本篇将带你实现一个番茄钟倒计时应用,用户可以设置专注时间和休息时间的时长,点击“开始专注”或“开始休息”按钮启动计时,应用会在倒计时竣事时进行提醒。番茄钟应用对于管理时间、提升工作服从非常有资助,而且还会参加猫咪图片作为界面装饰,让体验更加风趣。


关键词



  • UI互动应用
  • 番茄钟
  • 倒计时器
  • 状态管理
  • 用户交互

一、功能说明

番茄钟倒计时应用答应用户设置专注时间和休息时间,专注时间用于工作,休息时间用于放松。应用通过倒计时表现当前剩余时间,并在倒计时竣事后提醒用户进行下一阶段。界面上还添加了一只猫咪图片作为装饰。

二、所需组件



  • @Entry 和 @Component 装饰器
  • Column 结构组件
  • Image 组件用于表现猫咪图片
  • Text 组件用于表现倒计时和阶段提示
  • Button 组件用于用户交互
  • TextInput 组件用于输入专注和休息时间
  • 定时器函数 setInterval 和 clearInterval 用于控制倒计时
  • @State 修饰符用于状态管理
项目结构



  • 项目名称:PomodoroTimerApp
  • 自界说组件名称:PomodoroTimerPage
  • 代码文件:PomodoroTimerPage.ets、Index.ets

三、代码实现

  1. // 文件名:PomodoroTimerPage.ets
  2. // 定义番茄钟倒计时页面组件
  3. @Component
  4. export struct PomodoroTimerPage {
  5.   @State focusTime: number = 25 * 60; // 默认专注时间为25分钟(单位:秒)
  6.   @State breakTime: number = 5 * 60; // 默认休息时间为5分钟(单位:秒)
  7.   @State timeLeft: number = this.focusTime; // 倒计时时间
  8.   @State isFocusMode: boolean = true; // 当前阶段:专注或休息
  9.   @State isCountingDown: boolean = false; // 倒计时状态
  10.   private timerId: number | null = null; // 定时器 ID
  11.   // 构建页面布局和组件
  12.   build() {
  13.     Column({ space: 20 }) { // 创建垂直布局容器,子组件间距为 20
  14.       // 显示猫咪图片
  15.       Image($r('app.media.cat'))
  16.         .width('22%')
  17.         .height('49%')
  18.         .margin({ bottom: 20 })
  19.         .alignSelf(ItemAlign.Center);
  20.       // 显示当前阶段
  21.       Text(this.isFocusMode ? '专注时间' : '休息时间')
  22.         .fontSize(24)
  23.         .fontWeight(FontWeight.Bold)
  24.         .alignSelf(ItemAlign.Center);
  25.       // 显示倒计时时间
  26.       Text(`剩余时间: ${this.formatTime(this.timeLeft)}`)
  27.         .fontSize(20)
  28.         .alignSelf(ItemAlign.Center)
  29.         .fontColor(this.timeLeft > 0 ? Color.Black : Color.Red);
  30.       // 开始倒计时按钮
  31.       Button(this.isCountingDown ? '暂停' : (this.isFocusMode ? '开始专注' : '开始休息'))
  32.         .onClick(() => {
  33.           if (this.isCountingDown) {
  34.             this.pauseCountdown();
  35.           } else {
  36.             this.startCountdown();
  37.           }
  38.         })
  39.         .fontSize(20)
  40.         .backgroundColor(this.isCountingDown ? Color.Red : (this.isFocusMode ? Color.Blue : Color.Green))
  41.         .fontColor(Color.White)
  42.         .margin({ top: 20 });
  43.       // 重置按钮
  44.       Button('重置')
  45.         .onClick(() => this.resetCountdown())
  46.         .fontSize(20)
  47.         .backgroundColor(Color.Gray)
  48.         .fontColor(Color.White)
  49.         .margin({ top: 10 });
  50.       // 设置专注时间和休息时间
  51.       Row({ space: 10 }) {
  52.         TextInput({ placeholder: '设置专注时间(分钟)' })
  53.           .type(InputType.Number)
  54.           .onChange((value: string) => {
  55.             this.focusTime = (parseInt(value) || 25) * 60;
  56.             if (this.isFocusMode) {
  57.               this.timeLeft = this.focusTime;
  58.             }
  59.           });
  60.         TextInput({ placeholder: '设置休息时间(分钟)' })
  61.           .type(InputType.Number)
  62.           .onChange((value: string) => {
  63.             this.breakTime = (parseInt(value) || 5) * 60;
  64.             if (!this.isFocusMode) {
  65.               this.timeLeft = this.breakTime;
  66.             }
  67.           })
  68.       }
  69.       .justifyContent(FlexAlign.Center)
  70.     }
  71.     .padding(20)
  72.     .width('100%')
  73.     .height('100%')
  74.     .alignItems(HorizontalAlign.Center);
  75.   }
  76.   // 格式化倒计时时间
  77.   private formatTime(seconds: number): string {
  78.     const minutes = Math.floor(seconds / 60);
  79.     const remainingSeconds = seconds % 60;
  80.     return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  81.   }
  82.   // 开始倒计时的方法
  83.   private startCountdown() {
  84.     this.isCountingDown = true;
  85.     this.timerId = setInterval(() => {
  86.       if (this.timeLeft > 0) {
  87.         this.timeLeft -= 1;
  88.       } else {
  89.         this.switchMode(); // 切换到下一个阶段
  90.       }
  91.     }, 1000);
  92.   }
  93.   // 暂停倒计时的方法
  94.   private pauseCountdown() {
  95.     this.isCountingDown = false;
  96.     if (this.timerId !== null) {
  97.       clearInterval(this.timerId);
  98.       this.timerId = null;
  99.     }
  100.   }
  101.   // 重置倒计时的方法
  102.   private resetCountdown() {
  103.     this.pauseCountdown();
  104.     this.timeLeft = this.isFocusMode ? this.focusTime : this.breakTime;
  105.   }
  106.   // 切换专注和休息阶段
  107.   private switchMode() {
  108.     this.pauseCountdown();
  109.     this.isFocusMode = !this.isFocusMode;
  110.     this.timeLeft = this.isFocusMode ? this.focusTime : this.breakTime;
  111.   }
  112. }
复制代码
  1. // 文件名:Index.ets
  2. // 导入番茄钟倒计时页面组件
  3. import { PomodoroTimerPage } from './PomodoroTimerPage'
  4. // 定义应用入口组件
  5. @Entry
  6. @Component
  7. struct Index {
  8.   build() {
  9.     Column() {
  10.       PomodoroTimerPage() // 引用番茄钟页面组件
  11.     }
  12.     .padding(20) // 设置页面内边距
  13.   }
  14. }
复制代码
  结果示例:用户可以设置专注和休息时间,点击“开始专注”或“开始休息”按钮后,倒计时将开始,倒计时竣事时主动切换到下一个阶段。页面包含猫咪图片装饰,提升用户的使用体验。
  

  
四、代码解读



  • @State timeLeft:保存倒计时剩余时间,倒计时每秒减少 1,当时间为 0 时切换到下一个阶段。
  • @State isFocusMode:用于标识当前是专注模式照旧休息模式,每当倒计时竣事时切换。
  • startCountdown() 方法:使用 setInterval 启动倒计时,每秒减少 timeLeft 值。
  • pauseCountdown() 方法:暂停倒计时并扫除定时器。
  • switchMode() 方法:倒计时竣事后切换到下一个阶段,并重新设置 timeLeft 为下一个阶段的时长。

五、优化建议


  • 增加音效提醒:在每个阶段切换时播放提示音,资助用户区分工作与休息。
  • 动画结果:在倒计时减少时添加动画结果,如进度条或颜色厘革。
  • 汗青纪录:纪录每次专注和休息的时长,资助用户回首和分析本身的时间使用环境。

六、相关知识点



  • 「Mac畅玩鸿蒙与硬件26」UI互动应用篇3 - 倒计时和提醒功能实现
  • 「Mac畅玩鸿蒙与硬件11」鸿蒙UI组件篇1 - Text 和 Button 组件详解

小结

本篇教程通过番茄钟倒计时应用的实现,展示了如何灵活使用状态管理和定时器控制来实现更复杂的时间管理功能,并联合专注和休息模式切换,资助用户公道安排时间,进步服从。

下一篇预告

在下一篇「UI互动应用篇10 - 拼图小游戏」中,我们将探索一个风趣的项目,展示如何联合状态和动态图片实现更丰富的用户互动体验。

上一篇: 「Mac畅玩鸿蒙与硬件31」UI互动应用篇8 - 自界说评分星级组件

下一篇:「Mac畅玩鸿蒙与硬件33」UI互动应用篇10 - 拼图小游戏



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

刘俊凯

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表