HarmonyOS DatePickerDialog是否可以指定显示年月?

现在日期弹窗是显示年月日三列,怎么可以指定显示年月

阅读 449
1 个回答

目前暂时没有属性可设置DatePickerDialog仅仅只展示年和月份的。

如果想实现只有年月的滑动选择器弹窗,可以使用TextPickerAPI自定义弹窗,range字段设置可选年月。

demo如下:

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

@Entry
@Component
struct DatePickerDialogDemo {
  private years: string[] = ['1990年','1991年','1992年','1993年','1994年','1995年','1996年','1997年','1998年','1999年',
    '2000年','2001年','2002年','2003年','2004年','2005年','2006年','2007年','2008年','2009年','2010年','2011年','2012年',
    '2013年','2014年','2015年','2016年','2017年','2018年','2019年','2020年','2021年','2022年','2023年','2024年','2025年',
    '2026年','2027年','2028年','2029年','2030年']
  private months: string[] = ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
  private multi: string[][] = [this.years, this.months]
  build() {
    Column() {
      TextPicker({ range: this.multi })
        .onChange((value: string | string[], index: number | number[]) => {
          console.info('TextPicker 多列:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index))
        }).margin(bott)
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进