ts-basic-components-datepicker.md 4.4 KB
Newer Older
Z
zengyawen 已提交
1 2
# DatePicker

Y
yamila 已提交
3
日期选择器组件,用于根据指定日期范围创建日期滑动选择器。
H
hebingxue 已提交
4

H
geshi  
HelloCrease 已提交
5
>  **说明:**
H
hebingxue 已提交
6
>
H
HelloCrease 已提交
7
>  该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
8 9 10 11 12 13 14 15 16


## 子组件




## 接口

K
kangchongtao 已提交
17
DatePicker(options?: {start?: Date, end?: Date, selected?: Date})
Z
zengyawen 已提交
18

19
根据指定范围的Date创建可以选择日期的滑动选择器。
Z
zengyawen 已提交
20

K
kangchongtao 已提交
21
**参数:**
22

L
limeng 已提交
23 24 25 26 27
| 参数名   | 参数类型 | 必填 | 参数描述                                                     |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| start    | Date     | 否   | 指定选择器的起始日期。<br/>默认值:Date('1970-1-1')          |
| end      | Date     | 否   | 指定选择器的结束日期。<br/>默认值:Date('2100-12-31')        |
| selected | Date     | 否   | 设置选中项的日期。<br/>默认值:当前系统日期<br />从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。 |
Z
zengyawen 已提交
28 29 30

## 属性

Y
yamila 已提交
31 32
除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:

Y
yamila 已提交
33 34 35
| 名称                             | 参数类型                                      | 描述                                                         |
| -------------------------------- | --------------------------------------------- | ------------------------------------------------------------ |
| lunar                            | boolean                                       | 日期是否显示农历。<br/>-&nbsp;true:展示农历。<br/>-&nbsp;false:不展示农历。<br/>默认值:false |
Y
yamila 已提交
36 37 38
| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](#pickertextstyle10类型说明) | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br>size: '14fp', <br/>weight: FontWeight.Regular<br/>}<br/>} |
| textStyle<sup>10+</sup>          | [PickerTextStyle](#pickertextstyle10类型说明) | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br/>size: '16fp', <br/>weight: FontWeight.Regular<br/>}<br/>} |
| selectedTextStyle<sup>10+</sup>  | [PickerTextStyle](#pickertextstyle10类型说明) | 设置选中项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff007dff',<br/>font: {<br/>size: '20vp', <br/>weight: FontWeight.Medium<br/>}<br/>} |
Z
zengyawen 已提交
39

C
update  
chensi10 已提交
40 41
## PickerTextStyle<sup>10+</sup>类型说明

H
HelloCrease 已提交
42 43 44 45
| 参数名   | 参数类型                                     | 必填   | 参数描述                      |
| ----- | ---------------------------------------- | ---- | ------------------------- |
| color | [ResourceColor](ts-types.md#resourcecolor) | 否    | 文本颜色。                     |
| font  | [Font](ts-types.md#font)                 | 否    | 文本样式,picker只支持字号、字体粗细的设置。 |
Z
zengyawen 已提交
46 47 48

## 事件

Y
yamila 已提交
49 50
除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件:

H
HelloCrease 已提交
51 52
| 名称                                       | 功能描述        |
| ---------------------------------------- | ----------- |
Y
yamila 已提交
53
| onChange(callback:&nbsp;(value:&nbsp;DatePickerResult)&nbsp;=&gt;&nbsp;void) | 选择日期时触发该事件。 |
Z
zengyawen 已提交
54

H
hebingxue 已提交
55 56
## DatePickerResult对象说明

H
HelloCrease 已提交
57 58 59
| 名称    | 参数类型   | 描述                          |
| ----- | ------ | --------------------------- |
| year  | number | 选中日期的年。                     |
Y
yamila 已提交
60
| month | number | 选中日期的月(0~11),0表示1月,11表示12月。 |
H
HelloCrease 已提交
61
| day   | number | 选中日期的日。                     |
Z
zengyawen 已提交
62 63 64 65 66


## 示例


H
geshi  
HelloCrease 已提交
67 68
```ts
// xxx.ets
Z
zengyawen 已提交
69 70
@Entry
@Component
L
luoying_ace_admin 已提交
71 72
struct DatePickerExample {
  @State isLunar: boolean = false
Z
zengyawen 已提交
73 74 75 76
  private selectedDate: Date = new Date('2021-08-08')

  build() {
    Column() {
L
luoying_ace_admin 已提交
77
      Button('切换公历农历')
H
HelloCrease 已提交
78
        .margin({ top: 30, bottom: 30 })
L
luoying_ace_admin 已提交
79 80 81
        .onClick(() => {
          this.isLunar = !this.isLunar
        })
Z
zengyawen 已提交
82 83
      DatePicker({
        start: new Date('1970-1-1'),
84
        end: new Date('2100-1-1'),
85
        selected: this.selectedDate
Z
zengyawen 已提交
86
      })
L
luoying_ace_admin 已提交
87 88 89 90 91
        .lunar(this.isLunar)
        .onChange((value: DatePickerResult) => {
          this.selectedDate.setFullYear(value.year, value.month, value.day)
          console.info('select current date is: ' + JSON.stringify(value))
        })
Z
zengyawen 已提交
92 93 94 95 96 97

    }.width('100%')
  }
}
```

Y
yamila 已提交
98
![datePicker](figures/datePicker.gif)