harmony 鸿蒙TimePicker

  • 2022-08-09
  • 浏览 (675)

TimePicker

时间选择组件,根据指定参数创建选择器,支持选择小时及分钟。

说明:

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

子组件

接口

TimePicker(options?: {selected?: Date})

默认以24小时的时间区间创建滑动选择器。

参数:

参数名 参数类型 必填 参数描述
selected Date 设置选中项的时间。
默认值:当前系统时间
从API version 10开始,该参数支持$$双向绑定变量。

属性

除支持通用属性外,还支持以下属性:

名称 参数类型 描述
useMilitaryTime boolean 展示时间是否为24小时制。
默认值:false
说明:当展示时间为12小时制时,上下午与小时无联动关系。
disappearTextStyle10+ PickerTextStyle 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。
默认值:
{
color: ‘#ff182431’,
font: {
size: ‘14fp’,
weight: FontWeight.Regular
}
}
textStyle10+ PickerTextStyle 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。
默认值:
{
color: ‘#ff182431’,
font: {
size: ‘16fp’,
weight: FontWeight.Regular
}
}
selectedTextStyle10+ PickerTextStyle 设置选中项的文本颜色、字号、字体粗细。
默认值:
{
color: ‘#ff007dff’,
font: {
size: ‘20vp’,
weight: FontWeight.Medium
}
}

事件

除支持通用事件外,还支持以下事件:

名称 功能描述
onChange(callback: (value: TimePickerResult ) => void) 选择时间时触发该事件。

TimePickerResult对象说明

返回值为24小时制时间。

名称 参数类型 描述
hour number 选中时间的时。
取值范围:[0-23]
minute number 选中时间的分。
取值范围:[0-59]

示例

// xxx.ets
@Entry
@Component
struct TimePickerExample {
  @State isMilitaryTime: boolean = false
  private selectedTime: Date = new Date('2022-07-22T08:00:00')

  build() {
    Column() {
      Button('切换12小时制/24小时制')
        .margin({ top: 30 })
        .onClick(() => {
          this.isMilitaryTime = !this.isMilitaryTime
        })
      TimePicker({
        selected: this.selectedTime,
      })
        .useMilitaryTime(this.isMilitaryTime)
        .onChange((value: TimePickerResult) => {
          if(value.hour) {
            this.selectedTime.setHours(value.hour, value.minute)
            console.info('select current date is: ' + JSON.stringify(value))
          }
        })
        .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}})
        .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}})
        .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}})
    }.width('100%')
  }
}

timePicker

你可能感兴趣的鸿蒙文章

harmony 鸿蒙基于ArkTS的声明式开发范式

harmony 鸿蒙@ohos.arkui.advanced.Counter(计数器组件)

harmony 鸿蒙@ohos.arkui.advanced.SegmentButton(分段按钮)

harmony 鸿蒙@ohos.multimedia.avCastPicker (投播组件)

harmony 鸿蒙属性动画

harmony 鸿蒙枚举说明

harmony 鸿蒙Blank

harmony 鸿蒙Button

harmony 鸿蒙CalendarPicker

harmony 鸿蒙Checkbox

0  赞