HarmonyOS DatePickerDialog 怎么关闭循环日期?

5.0的,DatePickerDialog控件展示出来的日期会循环展示

问题1:怎么取消循环展示

问题2:怎么让月份以及日前面显示前置“0”

阅读 632
1 个回答

1、DatePicker中不支持关闭循环,没有相关的API接口可以实现该功能。

当前规避的手段只能通过TextPicker自定义一个日期组件,TextPicker中有.canLoop(false)可以关闭循环选择。

2、您可以通过TextPicker自定义实现月份显示前置0,可参考demo

class bottom {
  bottom:number = 50
}
let bott:bottom = new bottom()

@Entry
@Component
struct DatePickerDialogDemo {
  aboutToAppear() {
       for (let j = 1990; j <= 2030; j++) {
          this.years.push(''+j+'年')
      }
    for (let z = 1; z <= 12; z++) {
      if(z<10){
        this.months.push('0'+z+'月')
      }else{
        this.months.push(''+z+'月')
      }
    }
  }

  private years: string[]=[]
  private months: string[]=[]
  private multi: string[][] = [this.years, this.months]
  build() {
    Column() {
      TextPicker({ range: this.multi })
        .canLoop(false)
        .onChange((value: string | string[], index: number | number[]) => {
          console.info('TextPicker 多列:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index))
        }).margin(bott)
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进