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

鸿蒙(HarmonyOS)自定义Dialog实现时间选择控件

一、操作环境

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

二、效果图

三、代码

SelectedDateDialog.ets文件
/*** 时间选择*/
@CustomDialog
export struct SelectedDateDialog {@State selectedDate: Date = new Date()private callback: (value: DatePickerResult) => void@State value: DatePickerResult = {year: new Date().getFullYear(),month: new Date().getMonth(),day: parseInt(new Date().getDate().toString().padStart(2, '0')) }controller: CustomDialogController//自定义dialog必须声明aboutToAppear() {console.log("时间选择Loading展示》》》》》")}build() {Column() {DatePicker({start: new Date('2014-1-1'), //起始时间end: new Date('2030-1-1'), //结束时间selected: this.selectedDate, //默认所选时间}).margin({ top: 20 }).lunar(false) //日期是否显示农历。 - true:展示农历。 - false:不展示农历。 默认值:false.onChange(value => {console.log("轮换结果:" + value)this.value = value})Blank()Button('确定').borderRadius('3').width('30%').margin({ bottom: 20 }).onClick((event: ClickEvent) => {console.log("时间:" + this.value.year + this.value.month + this.value.day)this.callback?.(this.value)this.controller.close()})}.width('100%').height('50%')}
}

在page中的调用方式:

selectTimeDialog: CustomDialogController = new CustomDialogController({builder: SelectedDateDialog({ callback: (value) => {console.log("选择结果:" + value.year)this.changeTime = value.year + "-" + (value.month + 1) + "-" + value.day//月份从0开始,故需+1} }),cancel: this.closeDialog, //点击空白区域回调autoCancel: true,alignment: DialogAlignment.Bottom,offset: { dx: 0, dy: -20 },gridCount: 4,customStyle: false})closeDialog() {console.info('Click the callback in the blank area')this.selectTimeDialog.close()}openDialog() {//点击事件中直接调用this.selectTimeDialog.open()}

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

相关文章:

  • 学习008-02-04-08 Localize UI Elements(本地化UI元素)
  • 如何系统的学习C++和自动驾驶算法
  • typescript 定义类
  • 认证授权概述和SpringSecurity安全框架快速入门
  • docker常用命令集锦
  • 学习Java的日子 Day56 数据库连接池,Druid连接池
  • 如何实现PostgreSQL对某一张表的WAL日志进行记录
  • 机器学习数学基础(2)--最大似然函数
  • 详解 @RequestHeader 注解在 Spring Boot 中的使用
  • C# 表达式树的简介与说明
  • 【北京迅为】《i.MX8MM嵌入式Linux开发指南》-第三篇 嵌入式Linux驱动开发篇-第六十三章 输入子系统实验
  • [补题记录]Leetcode 15. 三数之和
  • 什么是sql注入攻击,如何预防介绍一下mysql中的常见数据类型
  • 史上最全的Seata教学并且连接springcloudAlibaba进行使用
  • InternLM Git 基础知识
  • 【Unity模型】古代亚洲建筑
  • 木马后门实验
  • 【React】useState:状态更新规则详解
  • C#中的异步编程:Task、Await 和 Async
  • SSRF-labs-master靶场
  • HBuilder X中配置vue-cli项目和UI库
  • 如何用PostMan按照规律进行循环访问接口
  • 稳态准直太阳光模拟器仪器光伏电池组件IV测试
  • vue3 reactive原理(二)-代理Set和Map及ref原理
  • Python自然语言处理库之NLTK与spaCy使用详解
  • Hive-内部表和外部表
  • Java并发编程(三)
  • Flink Doirs Connector 常见问题:Doris目前不支持流读
  • 期末复习资料——计算机系统基础
  • 一天搞定Recat(5)——ReactRouter(上)【已完结】