当前位置:   article > 正文

100个openharmony开源demo:1.日历

100个openharmony开源demo:1.日历

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

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

1.目录结构

2.主页代码

  1. import common from '@ohos.app.ability.common'
  2. import window from '@ohos.window'
  3. import { DataManager } from '../tools/DataManager'
  4. @Entry
  5. @Component
  6. struct Index {
  7. currentDate: Date = new Date()
  8. @State dataManager: DataManager = new DataManager(
  9. this.currentDate.getFullYear(),
  10. (this.currentDate.getMonth()+1),
  11. this.currentDate.getDate()
  12. )
  13. private context = getContext(this) as common.UIAbilityContext
  14. screenWidth: number = 480
  15. screenHeight: number = 853.33
  16. aboutToAppear() {
  17. // 获取屏幕的宽高
  18. window.getLastWindow(this.context)
  19. .then((windowClass: window.Window) => {
  20. let windowProperties = windowClass.getWindowProperties()
  21. this.screenWidth = px2vp(windowProperties.windowRect.width)
  22. this.screenHeight = px2vp(windowProperties.windowRect.height)
  23. this.dataManager.initData()
  24. })
  25. .catch((error: Error) => {
  26. console.error('Failed to obtain the window size. Cause: ' + JSON.stringify(error))
  27. })
  28. }
  29. build() {
  30. Column() {
  31. Column() {
  32. Row(){
  33. Row(){
  34. Image($r('app.media.arrow_left_double'))
  35. .height('60%')
  36. .margin({left:20})
  37. .onClick(() => {
  38. this.dataManager.currentYear = this.dataManager.currentYear - 1
  39. this.dataManager.initData();
  40. })
  41. Image($r('app.media.arrow_left'))
  42. .height('52%')
  43. .margin({left:10})
  44. .onClick(() => {
  45. if(this.dataManager.currentMonth > 1){
  46. this.dataManager.currentMonth = this.dataManager.currentMonth - 1
  47. }else{
  48. this.dataManager.currentYear = this.dataManager.currentYear - 1
  49. this.dataManager.currentMonth = 12
  50. }
  51. this.dataManager.initData();
  52. })
  53. }
  54. .margin({top:8})
  55. .width('30%')
  56. .height(((this.screenWidth-5*8)/7))
  57. .backgroundColor(0xF7F7F7)
  58. Text(this.dataManager.currentDateStr)
  59. .height('100%')
  60. .fontSize(((this.screenWidth-5*8)/7)/3)
  61. .fontWeight(FontWeight.Bold)
  62. Row(){
  63. Image($r('app.media.arrow_right'))
  64. .height('52%')
  65. .margin({right:10})
  66. .onClick(() => {
  67. if(this.dataManager.currentMonth < 12){
  68. this.dataManager.currentMonth = this.dataManager.currentMonth + 1
  69. }else{
  70. this.dataManager.currentYear = this.dataManager.currentYear + 1
  71. this.dataManager.currentMonth = 1
  72. }
  73. this.dataManager.initData();
  74. })
  75. Image($r('app.media.arrow_right_double'))
  76. .height('60%')
  77. .margin({right:20})
  78. .onClick(() => {
  79. this.dataManager.currentYear = this.dataManager.currentYear + 1;
  80. this.dataManager.initData();
  81. })
  82. }
  83. .margin({top:8})
  84. .width('30%')
  85. .height(((this.screenWidth-5*8)/7))
  86. .backgroundColor(0xF7F7F7)
  87. .justifyContent(FlexAlign.End)
  88. }
  89. .width('100%')
  90. .height(((this.screenWidth-5*8)/7))
  91. .backgroundColor(0xF7F7F7)
  92. .justifyContent(FlexAlign.SpaceBetween)
  93. Grid() {
  94. ForEach(this.dataManager.week, (day: any) => {
  95. GridItem() {
  96. Text(day.text)
  97. .fontSize(((this.screenWidth-5*8)/7)/3)
  98. .fontColor(day.fontColor)
  99. .backgroundColor(day.color)
  100. .width('100%')
  101. .height('100%')
  102. .textAlign(TextAlign.Center)
  103. .borderRadius(8)
  104. }
  105. .height("100%")
  106. }, day => day.id)
  107. }
  108. .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr')
  109. .rowsTemplate('1fr')
  110. .backgroundColor(0xF7F7F7)
  111. .columnsGap(5)
  112. .rowsGap(5)
  113. .height(((this.screenWidth-5*8)/7))
  114. Grid() {
  115. ForEach(this.dataManager.days, (day: any) => {
  116. GridItem() {
  117. Column() {
  118. Text(day.text)
  119. .fontSize(((this.screenWidth - 5 * 8) / 7) / 3)
  120. .fontColor(day.fontColor)
  121. .width('100%')
  122. .height('42%')
  123. .margin({top:8})
  124. .textAlign(TextAlign.Center)
  125. Text(day.lunarText)
  126. .fontSize(((this.screenWidth - 5 * 8) / 7) / 4)
  127. .fontColor(day.lunarFontColor)
  128. .width('100%')
  129. .height('30%')
  130. .textAlign(TextAlign.Center)
  131. }
  132. .borderRadius(day.borderRadius)
  133. .backgroundColor(day.color)
  134. .width('100%')
  135. .height(((this.screenWidth-5*8)/7))
  136. }
  137. }, day => day.id)
  138. }
  139. .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr')
  140. .rowsTemplate(this.dataManager.rowsTemplate)
  141. .columnsGap(5)
  142. .rowsGap(5)
  143. .height(((this.screenWidth-5*8)/7)*this.dataManager.rowsTemplateNumber)
  144. .margin(5)
  145. }
  146. .width('100%')
  147. .height('100%')
  148. .backgroundColor('0xFFFFFF')
  149. .borderRadius(8)
  150. }
  151. .width('100%')
  152. .height('100%')
  153. }
  154. }

3.显示效果

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

5.源码地址

九流下半/openharmony_100_projects

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/317083
推荐阅读