ts-methods-datepicker-dialog.md 3.5 KB
Newer Older
1
# 日期滑动选择器弹窗
2 3 4 5

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

6
根据指定范围的Date创建可以选择日期的滑动选择器,展示在弹窗上。
7 8 9 10 11 12 13

## 权限列表



## DatePickerDialog.show

14
show(options?: DatePickerDialogOptions)
15

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

18
- options参数
19 20 21 22
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
  | -------- | -------- | -------- | -------- | -------- |
  | start | Date | 否 | Date('1970-1-1') | 指定选择器的起始日期。 |
  | end | Date | 否 | Date('2100-12-31') | 指定选择器的结束日期。 |
23
  | selected | Date | 否 | 当前系统日期 | 设置选中项的日期。 |
24
  | lunar | boolean | 否 | false | 日期是否显示农历。 |
25
  | onAccept | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult对象说明)) => void | 否 | - | 点击弹窗中确定按钮时触发。 |
26
  | onCancel | () => void | 否 | - | 点击弹窗中取消按钮时触发。 |
27
  | onChange | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult对象说明)) => void | 否 | - | 滑动选择器,当前选择项改变时触发。 |
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

## 示例

### 日期滑动选择器(显示农历)示例
```
@Entry
@Component
struct DatePickerDialogExample01 {
  @State isLunar: boolean = true
  selectedDate: Date = new Date("2000-1-1")

  build() {
    Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center,
      justifyContent: FlexAlign.Center }) {
      Button("DatePickerDialog").onClick(() => {
        DatePickerDialog.show({
          start: new Date("2000-1-1"),
          end: new Date("2005-1-1"),
          selected: this.selectedDate,
          lunar: this.isLunar,
          onAccept: (value: DatePickerResult) => {
49
            this.selectedDate.setFullYear(value.year, value.month, value.day)
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
            console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
            }
          },
          onCancel: () => {
            console.info("DatePickerDialog:onCancel()")
          },
          onChange: (value: DatePickerResult) => {
            console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
          }
        })
      })
    }
  }
}
```
### 日期滑动选择器(不显示农历)示例
```
@Entry
@Component
struct DatePickerDialogExample02 {
  @State isLunar: boolean = false
  selectedDate: Date = new Date("2000-1-1")

  build() {
    Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center,
      justifyContent: FlexAlign.Center }) {
      Button("DatePickerDialog").onClick(() => {
        DatePickerDialog.show({
          start: new Date("2000-1-1"),
          end: new Date("2005-1-1"),
          selected: this.selectedDate,
          lunar: this.isLunar,
          onAccept: (value: DatePickerResult) => {
83
            this.selectedDate.setFullYear(value.year, value.month, value.day)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
            }
          },
          onCancel: () => {
            console.info("DatePickerDialog:onCancel()")
          },
          onChange: (value: DatePickerResult) => {
            console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
          }
        })
      })
    }
  }
}
```