日期滑动选择器弹窗


日期滑动选择器弹窗

根据指定的日期范围创建日期滑动选择器,展示在弹窗上。

说明:

该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

DatePickerDialog.show

show(options?: DatePickerDialogOptions)

定义日期滑动选择器弹窗并弹出。

DatePickerDialogOptions参数:

参数名 参数类型 必填 默认值 参数描述
start Date Date('1970-1-1') 设置选择器的起始日期。
end Date Date('2100-12-31') 设置选择器的结束日期。
selected Date 当前系统日期 设置当前选中的日期。
lunar boolean false 日期是否显示为农历。
showTime10+ boolean false 是否展示时间项。
useMilitaryTime10+ boolean false 展示时间是否为24小时制。
disappearTextStyle10+ PickerTextStyle - 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。
textStyle10+ PickerTextStyle - 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。
selectedTextStyle10+ PickerTextStyle - 设置选中项的文本颜色、字号、字体粗细。
onAccept (value: DatePickerResult) => void - 点击弹窗中的“确定”按钮时触发该回调。
onCancel () => void - 点击弹窗中的“取消”按钮时触发该回调。
onChange (value: DatePickerResult) => void - 滑动弹窗中的滑动选择器使当前选中项改变时触发该回调。

示例

// xxx.ets
@Entry
@Component
struct DatePickerDialogExample {
  selectedDate: Date = new Date("2010-1-1")

  build() {
    Column() {
      Button("DatePickerDialog")
        .margin(20)
        .onClick(() => {
          DatePickerDialog.show({
            start: new Date("2000-1-1"),
            end: new Date("2100-12-31"),
            selected: this.selectedDate,
            onAccept: (value: DatePickerResult) => {
              // 通过Date的setFullYear方法设置按下确定按钮时的日期,这样当弹窗再次弹出时显示选中的是上一次确定的日期
              this.selectedDate.setFullYear(value.year, value.month, value.day)
              console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
            },
            onCancel: () => {
              console.info("DatePickerDialog:onCancel()")
            },
            onChange: (value: DatePickerResult) => {
              console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
            }
          })
        })

      Button("Lunar DatePickerDialog")
        .margin(20)
        .onClick(() => {
          DatePickerDialog.show({
            start: new Date("2000-1-1"),
            end: new Date("2100-12-31"),
            selected: this.selectedDate,
            lunar: true,
            onAccept: (value: DatePickerResult) => {
              this.selectedDate.setFullYear(value.year, value.month, value.day)
              console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
            },
            onCancel: () => {
              console.info("DatePickerDialog:onCancel()")
            },
            onChange: (value: DatePickerResult) => {
              console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
            }
          })
        })
    }.width('100%')
  }
}
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