当前位置: 首页 > news >正文

鸿蒙(HarmonyOS)DatePicker+TimePicker时间选择控件

一、操作环境

操作系统:  Windows 11 专业版、IDE:DevEco Studio 3.1.1 Release、SDK:HarmonyOS 3.1.0(API 9)

二、效果图

       

可实现两种选择方式,可带时分选择,也可不带,使用更加方便。

三、代码

SelectedDateDialog.ets文件
/*** 时间选择*/
@CustomDialog
export struct SelectedDateDialog {private selectedDate: Dateprivate isShowTime: Visibility = Visibility.Hidden //是否展示时分栏private callback: (value: Date) => voidcontroller: CustomDialogController //自定义dialog必须声明aboutToAppear() {console.log("时间选择展示:" + JSON.stringify(this.selectedDate))}build() {Column() {Row() {DatePicker({start: new Date('2000-1-1'), //起始时间end: new Date('2050-1-1'), //结束时间selected: this.selectedDate //默认所选时间}).width(this.isShowTime == Visibility.Visible ? '60%' : '100%') //不显示时分,则宽为100.height('100%').lunar(false) //日期是否显示农历。 - true:展示农历。 - false:不展示农历。 默认值:false.onChange(value => {// console.log("轮换结果:" + JSON.stringify(value))this.selectedDate.setFullYear(value.year, value.month, value.day)})TimePicker({ selected: this.selectedDate }).width('35%').height('100%').visibility(this.isShowTime).useMilitaryTime(true) //24小时制.onChange(value => {// console.info('select current date is: ' + JSON.stringify(value))if (value.hour >= 0) {this.selectedDate.setHours(value.hour, value.minute)}})}.width('100%').height('80%')Blank()Button('确定').borderRadius('3').width('40%').margin({ bottom: 20 }).onClick((event: ClickEvent) => {console.log('选择时间:' + JSON.stringify(this.selectedDate))this.callback?.(this.selectedDate)this.controller.close()})}.width('100%').height('50%')}
}

使用其中的参数isShowTime控制是否显示时分栏的选择,Visibility.Visible为显示,Visibility.Hidden不显示

在page中的调用方式:

 @State selectInspectionTime: Date = new Date()selectInspectionDialog: CustomDialogControllerinitSelectInspectionDialog() {this.selectInspectionDialog = new CustomDialogController({builder: SelectedDateDialog({ selectedDate: this.selectInspectionTime, isShowTime: Visibility.Visible,callback: (value) => {let time = value.getFullYear() + "-" + (value.getMonth() + 1) + "-" + value.getDate()+ "T" + value.getHours() + ":" + value.getMinutes() //月份从0开始,故需+1} }),cancel: this.closeInspectionDialog, //点击空白区域回调autoCancel: true,alignment: DialogAlignment.Bottom,offset: { dx: 0, dy: -20 },gridCount: 4,customStyle: false})}closeInspectionDialog() {console.info('Click the callback in the blank area')this.selectInspectionDialog.close()}openDialog() {//点击事件调用打开dialogthis.selectInspectionDialog.open()}

http://www.lryc.cn/news/411443.html

相关文章:

  • 2024年和2025年CFA FRM CAIA ESG自己整理的资料
  • AMD第二季度财报:数据中心产品销售激增,接近总收入一半
  • ThreadLocal详解及ThreadLocal源码分析
  • FastGPT、Dify、Coze产品功能对比分析
  • 【Linux】缓冲区的理解
  • 基于单片机的电梯控制系统的设计
  • IP-GUARD文档云备份服务器迁移数据操作说明
  • linux常用命令ls详细说明
  • Python3网络爬虫开发实战(4)数据的存储
  • 《C++基础入门与实战进阶》专栏介绍
  • 每天一个数据分析题(四百五十)- 数据清洗
  • 昇思25天学习打卡营第XX天|Pix2Pix实现图像转换
  • 数据结构经典测试题5
  • React Native初次使用遇到的问题
  • 2024西安铁一中集训DAY28 ---- 模拟赛(简单dp + 堆,模拟 + 点分治 + 神秘dp)
  • 【论文阅读笔记 + 思考 + 总结】MoMask: Generative Masked Modeling of 3D Human Motions
  • Mojo控制语句详解
  • web安全基础学习
  • 天气预报的爬虫内容打印并存储用户操作
  • OrangePi AI Pro 固件升级 —— 让主频从 1.0 GHz 到 1.6 GHz 的巨大升级
  • 学习大数据DAY27 Linux最终阶段测试
  • ctr管理containerd基本命令
  • rust 初探 -- 路径(path)
  • XXE -靶机
  • vue2 搭配 html2canvas 截图并设置截图时样式(不影响页面) 以及 base64转file文件上传 或者下载截图 小记
  • 请大家监督:我要开启Python之路,首要任务最简单的搭建环境
  • http协议深度解析——网络时代的安全与效率(1)
  • 类和对象【下】
  • 面向未来的S2B2C电商供应链系统发展趋势与创新探索
  • 【C++】哈希容器