网站的优化承诺,上海营销网站建设公司,wordpress图片批量设置,惠州网站建设系统本篇将带你实现一个简易日历应用#xff0c;显示当前月份的日期#xff0c;并支持选择特定日期的功能。用户可以通过点击日期高亮选中#xff0c;还可以切换上下月份#xff0c;体验动态界面的交互效果。 关键词
UI互动应用简易日历动态界面状态管理用户交互 一、功能说明…本篇将带你实现一个简易日历应用显示当前月份的日期并支持选择特定日期的功能。用户可以通过点击日期高亮选中还可以切换上下月份体验动态界面的交互效果。 关键词
UI互动应用简易日历动态界面状态管理用户交互 一、功能说明
简易日历应用提供了以下功能
查看当前月份的日期。点击某一天后高亮显示选中的日期。支持切换到上一个或下一个月份。
用户通过动态界面交互实时查看和选择日期提升应用的互动体验。 二、所需组件
Entry 和 Component 装饰器Column 和 Row 布局组件用于页面和网格布局Button 组件用于切换月份和选择日期Text 组件用于显示标题和选中的日期State 修饰符用于管理界面动态更新的状态 三、项目结构
项目名称SimpleCalendarApp自定义组件名称SimpleCalendarPage代码文件 SimpleCalendarPage.ets实现核心逻辑。Index.ets作为应用入口调用主页面组件。 四、代码实现
// 文件名SimpleCalendarPage.etsComponent
export struct SimpleCalendarPage {State currentDate: Date new Date(); // 当前日期State selectedDate: Date | null null; // 选中的日期build() {Column({ space: 20 }) {// 显示当前年份和月份Row({ space: 10 }) {Button(上一个月).onClick(() this.changeMonth(-1));Text(${this.currentDate.getFullYear()}年 ${this.currentDate.getMonth() 1}月).fontSize(24).fontWeight(FontWeight.Bold).alignSelf(ItemAlign.Center);Button(下一个月).onClick(() this.changeMonth(1));}.justifyContent(FlexAlign.SpaceBetween);// 显示星期标题Row({ space: 5 }) {ForEach([日, 一, 二, 三, 四, 五, 六], (day: string) {Text(day).fontSize(18).fontWeight(FontWeight.Bold).width(12%).textAlign(TextAlign.Center);});}// 显示日期Column({ space: 5 }) {ForEach(this.getDatesInMonth(), (week: (Date | null)[]) {Row({ space: 10 }) {ForEach(week, (date: Date | null) {if (date) {Button(date.getDate().toString()).backgroundColor(this.selectedDate this.isSameDay(this.selectedDate, date)? Color.Blue: Color.Gray).fontColor(Color.White).onClick(() this.selectedDate date).width(12%).height(50px);} else {Text( )// 空白占位.width(12%).height(50px);}});}.justifyContent(FlexAlign.SpaceBetween);});}// 显示选中日期if (this.selectedDate) {Text(选中的日期${this.selectedDate.toLocaleDateString()}).fontSize(20).alignSelf(ItemAlign.Center);}}.padding(20).width(100%).height(100%).alignItems(HorizontalAlign.Center);}// 获取当月的所有日期按星期分组private getDatesInMonth(): (Date | null)[][] {const year this.currentDate.getFullYear();const month this.currentDate.getMonth();const firstDay new Date(year, month, 1).getDay();const lastDay new Date(year, month 1, 0).getDate();const weeks: (Date | null)[][] [];let currentWeek: (Date | null)[] Array(firstDay).fill(null);for (let day 1; day lastDay; day) {currentWeek.push(new Date(year, month, day));if (currentWeek.length 7) {weeks.push(currentWeek);currentWeek [];}}if (currentWeek.length 0) {while (currentWeek.length 7) {currentWeek.push(null);}weeks.push(currentWeek);}return weeks;}// 判断两个日期是否为同一天private isSameDay(date1: Date, date2: Date): boolean {return (date1.getFullYear() date2.getFullYear() date1.getMonth() date2.getMonth() date1.getDate() date2.getDate());}// 切换月份private changeMonth(offset: number) {const newDate new Date(this.currentDate);newDate.setMonth(this.currentDate.getMonth() offset);this.currentDate newDate;this.selectedDate null; // 切换月份时清除选中状态}
}// 文件名Index.etsimport { SimpleCalendarPage } from ./SimpleCalendarPage;Entry
Component
struct Index {build() {Column() {SimpleCalendarPage() // 调用简易日历页面}.padding(20)}
}效果示例用户可以通过按钮切换月份并点击某一天高亮选中。界面实时更新选中的日期显示在屏幕下方。 五、代码解读
状态管理State currentDate 和 State selectedDate 用于保存当前显示的月份和选中的日期。动态生成日期按钮通过 getDatesInMonth 方法动态生成当月的所有日期使用 ForEach 渲染按钮。日期高亮显示通过 isSameDay 方法判断是否选中某一天并更新按钮的背景色。月份切换逻辑通过 changeMonth 方法更新 currentDate 并重新计算日期。 六、优化建议
添加工作日和周末标记用不同颜色标记工作日和周末增加视觉提示。支持跳转到指定日期提供输入框或日期选择器快速跳转到特定月份。扩展交互功能例如显示当月的节假日信息或增加任务记录功能。 七、相关知识点
「Mac畅玩鸿蒙与硬件11」鸿蒙 UI 组件篇1 - Text 和 Button 组件详解「Mac畅玩鸿蒙与硬件33」UI互动应用篇10 - 数字猜谜游戏 小结
本篇通过动态生成日历网格展示了如何结合状态管理和用户交互实现简易日历功能。用户可通过按钮切换月份并高亮选中日期体验鸿蒙系统的开发便捷性。 下一篇预告
在下一篇「UI互动应用篇13 - 数字滚动抽奖器」中我们将探索如何设计一个富有趣味的抽奖应用。你将学习如何实现数字滚动效果打造一个动态变化的抽奖界面增加用户交互的趣味性与吸引力。 上一篇 「Mac畅玩鸿蒙与硬件34」UI互动应用篇11 - 颜色选择器
下一篇 「Mac畅玩鸿蒙与硬件36」UI互动应用篇13 - 数字滚动抽奖器 作者SoraLuna 链接https://www.nutpi.net/thread?topicId312 來源坚果派 著作权归作者所有。商业转载请联系作者获得授权非商业转载请注明出处。
文章转载自: http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.jlnlr.cn.gov.cn.jlnlr.cn http://www.morning.jmwrj.cn.gov.cn.jmwrj.cn http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.xqjrg.cn.gov.cn.xqjrg.cn http://www.morning.kphsp.cn.gov.cn.kphsp.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.tzzfy.cn.gov.cn.tzzfy.cn http://www.morning.znlhc.cn.gov.cn.znlhc.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.mjctt.cn.gov.cn.mjctt.cn http://www.morning.pfntr.cn.gov.cn.pfntr.cn http://www.morning.syhwc.cn.gov.cn.syhwc.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.wchcx.cn.gov.cn.wchcx.cn http://www.morning.npmcf.cn.gov.cn.npmcf.cn http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.snygg.cn.gov.cn.snygg.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.nxstj.cn.gov.cn.nxstj.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.niukaji.com.gov.cn.niukaji.com http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn http://www.morning.wnbqy.cn.gov.cn.wnbqy.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.ysbhj.cn.gov.cn.ysbhj.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.zlnyk.cn.gov.cn.zlnyk.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.nsfxt.cn.gov.cn.nsfxt.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.pqppj.cn.gov.cn.pqppj.cn http://www.morning.xhxsr.cn.gov.cn.xhxsr.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.drgmr.cn.gov.cn.drgmr.cn http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn http://www.morning.kmldm.cn.gov.cn.kmldm.cn http://www.morning.prprz.cn.gov.cn.prprz.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.kbntl.cn.gov.cn.kbntl.cn http://www.morning.hsflq.cn.gov.cn.hsflq.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.jrdbq.cn.gov.cn.jrdbq.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.kbgzj.cn.gov.cn.kbgzj.cn http://www.morning.hrtct.cn.gov.cn.hrtct.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn