100个openharmony开源demo:1.日历

news/2024/7/10 18:59:52 标签: 开源, openharmony 日历, arkts

准备用开发者手机写100个开源的demo不知道能不能实现,日拱一卒,期待蜕变。

第一个demo:日历借鉴了网上的日历算法,自己用arkts写了界面和点击事件,各位可根据此demo写自己的日历选择器等组件。

1.目录结构:

2.主页代码

import common from '@ohos.app.ability.common'
import window from '@ohos.window'

import { DataManager } from '../tools/DataManager'

@Entry
@Component
struct Index {
  currentDate: Date = new Date()
  @State dataManager: DataManager = new DataManager(
    this.currentDate.getFullYear(),
    (this.currentDate.getMonth()+1),
    this.currentDate.getDate()
  )

  private context = getContext(this) as common.UIAbilityContext

  screenWidth: number = 480
  screenHeight: number = 853.33

  aboutToAppear() {
    // 获取屏幕的宽高
    window.getLastWindow(this.context)
      .then((windowClass: window.Window) => {
        let windowProperties = windowClass.getWindowProperties()
        this.screenWidth = px2vp(windowProperties.windowRect.width)
        this.screenHeight = px2vp(windowProperties.windowRect.height)
        this.dataManager.initData()
      })
      .catch((error: Error) => {
        console.error('Failed to obtain the window size. Cause: ' + JSON.stringify(error))
      })
  }

  build() {
    Column() {
      Column() {
        Row(){
          Row(){
            Image($r('app.media.arrow_left_double'))
              .height('60%')
              .margin({left:20})
              .onClick(() => {
                this.dataManager.currentYear = this.dataManager.currentYear - 1
                this.dataManager.initData();
              })
            Image($r('app.media.arrow_left'))
              .height('52%')
              .margin({left:10})
              .onClick(() => {
                if(this.dataManager.currentMonth > 1){
                  this.dataManager.currentMonth = this.dataManager.currentMonth - 1
                }else{
                  this.dataManager.currentYear = this.dataManager.currentYear - 1
                  this.dataManager.currentMonth = 12
                }
                this.dataManager.initData();
              })
          }
          .margin({top:8})
          .width('30%')
          .height(((this.screenWidth-5*8)/7))
          .backgroundColor(0xF7F7F7)

          Text(this.dataManager.currentDateStr)
            .height('100%')
            .fontSize(((this.screenWidth-5*8)/7)/3)
            .fontWeight(FontWeight.Bold)

          Row(){
            Image($r('app.media.arrow_right'))
              .height('52%')
              .margin({right:10})
              .onClick(() => {
                if(this.dataManager.currentMonth < 12){
                  this.dataManager.currentMonth = this.dataManager.currentMonth + 1
                }else{
                  this.dataManager.currentYear = this.dataManager.currentYear + 1
                  this.dataManager.currentMonth = 1
                }
                this.dataManager.initData();
              })
            Image($r('app.media.arrow_right_double'))
              .height('60%')
              .margin({right:20})
              .onClick(() => {
                this.dataManager.currentYear = this.dataManager.currentYear + 1;
                this.dataManager.initData();
              })
          }
          .margin({top:8})
          .width('30%')
          .height(((this.screenWidth-5*8)/7))
          .backgroundColor(0xF7F7F7)
          .justifyContent(FlexAlign.End)
        }
        .width('100%')
        .height(((this.screenWidth-5*8)/7))
        .backgroundColor(0xF7F7F7)
        .justifyContent(FlexAlign.SpaceBetween)

        Grid() {
          ForEach(this.dataManager.week, (day: any) => {
            GridItem() {
              Text(day.text)
                .fontSize(((this.screenWidth-5*8)/7)/3)
                .fontColor(day.fontColor)
                .backgroundColor(day.color)
                .width('100%')
                .height('100%')
                .textAlign(TextAlign.Center)
                .borderRadius(8)
            }
            .height("100%")
          }, day => day.id)
        }
        .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr')
        .rowsTemplate('1fr')
        .backgroundColor(0xF7F7F7)
        .columnsGap(5)
        .rowsGap(5)
        .height(((this.screenWidth-5*8)/7))

        Grid() {
          ForEach(this.dataManager.days, (day: any) => {
            GridItem() {
              Column() {
                Text(day.text)
                  .fontSize(((this.screenWidth - 5 * 8) / 7) / 3)
                  .fontColor(day.fontColor)
                  .width('100%')
                  .height('42%')
                  .margin({top:8})
                  .textAlign(TextAlign.Center)
                Text(day.lunarText)
                  .fontSize(((this.screenWidth - 5 * 8) / 7) / 4)
                  .fontColor(day.lunarFontColor)
                  .width('100%')
                  .height('30%')
                  .textAlign(TextAlign.Center)
              }
              .borderRadius(day.borderRadius)
              .backgroundColor(day.color)
              .width('100%')
              .height(((this.screenWidth-5*8)/7))
            }
          }, day => day.id)
        }
        .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr')
        .rowsTemplate(this.dataManager.rowsTemplate)
        .columnsGap(5)
        .rowsGap(5)
        .height(((this.screenWidth-5*8)/7)*this.dataManager.rowsTemplateNumber)
        .margin(5)
      }
      .width('100%')
      .height('100%')
      .backgroundColor('0xFFFFFF')
      .borderRadius(8)
    }
    .width('100%')
    .height('100%')
  }
}

3.显示效果

4.卡片显示(每晚00:01刷新)

5.源码地址

九流下半/openharmony_100_projects


http://www.niftyadmin.cn/n/5443854.html

相关文章

单片机--数电(4)

触发器 数字电路中&#xff1a;分组合逻辑电路与时序逻辑电路两大类 组合逻辑电路的基本单元是门电路&#xff08;与或非等一些门电路&#xff09; 时序逻辑电路的基本单元是触发器 触发器与门电路的区别 门电路某一时刻的输出信号完全取决于该时刻的输入信号&#xff0c;…

MIT的研究人员最近开发了一种名为“FeatUp”的新算法,这一突破性技术为计算机视觉领域带来了高分辨率的洞察力

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

Oracle安装navicat连接表空间创建数据导入导出

一&#xff0c;Oracle安装 oracle配置文本&#xff0c;菜单&#xff0c;工具栏 二&#xff0c;navicat连接 下载地址&#xff1a; 配环境 三&#xff0c;表空间创建 在PLsql使用sys账号的sysydata权限登录执行以下命令&#xff1a; 表空间 建立表空间(一般建N个存数据的表空间…

【Apache ShenYu源码】如何实现负载均衡模块设计

ShenYu是一个异步的&#xff0c;高性能的&#xff0c;跨语言的&#xff0c;响应式的 API 网关。有关ShenYu的介绍可以戳这。 一、前瞻 今天我们尝试不同的代码阅读方式&#xff0c;按模块来去阅读源码&#xff0c;看看效果如何。 本次阅读锁定在shenyu-loadbalancer&#xf…

Cron表达式浅析

文章目录 1 cron表达式介绍2 cron表达式语法介绍3 cron表达式阅读练习4 用途 1 cron表达式介绍 cron 表达式是一种用于指定定时任务执行时间的表达式&#xff0c;通常用于在 Unix/Linux 系统中设置定时任务。它由6个字段组成&#xff0c;分别表示秒、分钟、小时、日期、月份和…

gin | gin路由

gin 路由 基本路由 gin 框架中采用的路由库是基于 httprouter 做的&#xff1b;地址为&#xff1a;GitHub - julienschmidt/httprouter: A high performance HTTP request router that scales well package mainimport ("net/http""github.com/gin-gonic/gin…

macOS 通过 MacPorts 正确安装 MySQL 同时解决无法连接问题

如果你通过 sudo port install 命令正常安装了 MySQL&#xff0c;再通过 sudo port load 命令启动了 MySQL Server&#xff0c;此刻却发现使用 Navicat 之类的 GUI 软件无法连接&#xff0c;始终返回无法连接到 127.0.0.1 服务器。这是一个小坑&#xff0c;因为他默认使用了 So…

Scikit-Learn逻辑回归(二)

Scikit-Learn逻辑回归二&#xff1a;多项式与正则化 1、多项式回归回顾1.1、逻辑回归为什么要使用多项式1.2、多项式回归及原理 2、逻辑回归与多项式 1、多项式回归回顾 本文接上篇&#xff1a;Scikit-Learn逻辑回归(一) 上篇中&#xff0c;我们详细介绍了逻辑回归的概念、原理…