自定义日历组件,开发中,自定义生成需要的日历格式方法有哪些?

自定义日历组件



HarmonyOS
2024-05-26 12:37:19
1.8w浏览
已于2024-5-27 16:33:54修改
收藏 0
回答 1
回答 1
按赞同
/
按时间
elucky

开发中,自定义生成需要的日历格式,这边是以生成电脑里的日历样式为例,里面用到了三方库组件dayjs来获取当前的日期,通过Grid组件来生成日历的排版布局,本想加入luanr组件显示对应的农历,发现还没有对应的三方库,就未实现效果,基本代码已经写上。

使用的核心API

核心代码解释

关键就是获取到正确的dataList,我这边的思路是先找到显示的第一个日期,然后按照日期的顺序排满日历。

// import { Lunar } from 'lunar-javascript' 
import dateClass from '../beans/DateClass' 
import dayjs from 'dayjs' 
  
@Entry 
@Component 
struct DatePickerDialogExample01 { 
  weekList: Array<string> = ['一', '二', '三', '四', '五', '六', '日'] 
  @State show: boolean = false 
  @State selectDate: dayjs.Dayjs = dayjs() 
  @State selectMonth: string = dayjs().format('YYYY年MM月') 
  @State dataList: Array<dateClass> = [] 
  @State monthParam: number = 0 //切换日历的参数 
  
  build() { 
    Column() { 
      Button('弹出或关闭日历') 
        .height(36) 
        .fontSize(20) 
        .fontColor(Color.White) 
        .onClick(() => { 
          this.show = !this.show 
          this.selectDate = dayjs() 
          this.monthParam = 0 
          this.getDataList(this.monthParam) 
        }) 
  
      if (this.show) { 
        Row() { 
          Text(this.selectMonth) 
            .fontSize(16) 
            .fontColor(Color.White) 
          Row() { 
            Row() { 
              Image($r('app.media.icon_left')) 
                .width(24) 
                .height(12) 
                .backgroundColor(Color.Black) 
            } 
            .height('100%') 
            .width(48) 
            .alignItems(VerticalAlign.Center) 
            .justifyContent(FlexAlign.Center) 
            .onClick(() => { 
              this.monthParam-- 
              this.getDataList(this.monthParam) 
            }) 
  
            Row() { 
              Image($r('app.media.icon_right')) 
                .width(24) 
                .height(12) 
                .backgroundColor(Color.Black) 
            } 
            .height('100%') 
            .width(48) 
            .alignItems(VerticalAlign.Center) 
            .justifyContent(FlexAlign.Center) 
            .onClick(() => { 
              this.monthParam++ 
              this.getDataList(this.monthParam) 
            }) 
          } 
        } 
        .height(48) 
        .width('100%') 
        .justifyContent(FlexAlign.SpaceBetween) 
  
        Row() { 
          ForEach(this.weekList, (item: string) => { 
            Text(item) 
              .fontSize(16) 
              .fontColor(Color.White) 
              .width(48) 
              .textAlign(TextAlign.Center) 
          }) 
        } 
        .width('100%') 
  
        Grid() { 
          ForEach(this.dataList, (item: dateClass) => { 
            GridItem() { 
              Column() { 
                Text(item.day.toString()) 
                  .fontSize(16) 
                  .fontColor(Color.White) 
                  .width(48) 
                  .textAlign(TextAlign.Center) 
                  .opacity(item.isToMonth ? 1 : 0.4) 
                Text(item.lunarDay) 
                  .fontSize(12) 
                  .fontColor(Color.White) 
                  .width(48) 
                  .textAlign(TextAlign.Center) 
                  .opacity(item.isToMonth ? 1 : 0.4) 
              } 
              .width(48) 
              .height(48) 
              .alignItems(HorizontalAlign.Center) 
              .justifyContent(FlexAlign.Center) 
              .backgroundColor(item.dayjsObj.format('YYYY-MM-DD') == 
                               dayjs().format('YYYY-MM-DD') ? 
                '#007DFF' : Color.Black) 
              .borderWidth(1) 
              .borderColor(item.dayjsObj.format('YYYY-MM-DD') == 
                           this.selectDate.format('YYYY-MM-DD') ? 
                '#007DFF' : Color.Black) 
              .onClick(() => { 
                this.selectDate = item.dayjsObj 
              }) 
            } 
          }) 
        } 
        .height(288) 
        .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr') 
        .rowsTemplate('1fr 1fr 1fr 1fr 1fr 1fr') 
        .columnsGap(0) 
        .rowsGap(0) 
      } 
    } 
    .width('100%') 
    .height('100%') 
    .backgroundColor(Color.Black) 
    .justifyContent(FlexAlign.Center) 
    .padding({ left: 12, right: 12 }) 
  } 
  
  getDataList(param: number) { 
    this.dataList = [] 
    let firstDate = dayjs().add(param, 'month').startOf('month') // 当月的第一天 
    let afterDate = dayjs().add(param, 'month').endOf('month') // 当月的最后一天 
    this.selectMonth = firstDate.format('YYYY年MM月') 
    let frontDay = 0 // 显示日历的第一天需要向前的天数 
    if (firstDate.day() == 0) { 
      frontDay = 6 
    } else { 
      frontDay = firstDate.day() - 1 
    } 
    let showFirstDay = firstDate.subtract(frontDay, 'day') // 显示日历的第一天 
    for (let i = 0;i < 42; i++) { 
      let dayjsObj = showFirstDay.add(i, 'day') 
      let day = dayjsObj.date() 
      let lunarDay = '' 
      // if (Lunar.fromDate(dayjsObj.toDate()).getFestivals().length !== 0) { // 显示节日 
      //   lunarDay = Lunar.fromDate(dayjsObj.toDate()).getFestivals()[0] 
      // } else if (Lunar.fromDate(dayjsObj.toDate()).getJieQi() !== '') { // 显示节气 
      //   lunarDay = Lunar.fromDate(dayjsObj.toDate()).getJieQi() 
      // } else { // 显示农历日期 
      //   lunarDay = Lunar.fromDate(dayjsObj.toDate()).getDayInChinese() 
      // } 
      let isToMonth = true 
      if (dayjsObj.isBefore(firstDate) || dayjsObj.isAfter(afterDate)) { 
        isToMonth = false 
      } 
      this.dataList.push(new dateClass(dayjsObj, day, lunarDay, isToMonth)) 
    } 
    //        console.log('aboutToAppear',JSON.stringify(this.dataList)) 
  } 
  
  aboutToAppear() { 
    this.getDataList(this.monthParam) 
  } 
} 
  
//DateClass 类 
import dayjs from 'dayjs' 
  
export default class DateClass { 
  dayjsObj: dayjs.Dayjs  // dayjs时间 
  day: number // 天 
  lunarDay: string // 农历 
  isToMonth: boolean // 是否当前月 
  
  constructor(dayjsObj: dayjs.Dayjs, day: number, lunarDay: string, isToMonth: boolean) { 
    this.dayjsObj = dayjsObj 
    this.day = day 
    this.lunarDay = lunarDay 
    this.isToMonth = isToMonth 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.

实现效果

适配的版本信息

IDE:DevEco Studio 4.0.3.600

SDK:HarmoneyOS 4.0.10.11

分享
微博
QQ
微信
回复
2024-05-27 16:20:51


相关问题
HarmonyOS 自定义原生日历demo
584浏览 • 1回复 待解决
HarmonyOS 自定义生命周期问题
420浏览 • 1回复 待解决
HarmonyOS 自定义tabbar组件开发
402浏览 • 1回复 待解决
HarmonyOS 定义自定义组件
662浏览 • 1回复 待解决
js 自定义组件如何传递方法
6307浏览 • 2回复 待解决
自定义弹窗自定义转场动画
1629浏览 • 1回复 待解决
HarmonyOS自定义组件增加方法如何实现
980浏览 • 1回复 待解决
自定义组件如何添加图片?
3043浏览 • 1回复 待解决