1. 涉及到的技术点
- @Extend装饰器的使用
- List列表的使用
- 日期Date的使用
2. 官方文档指南
3. demo具体实现过程
- 自动获取今天,明天,后天的日期,首先定义一个DayInfo接口,用来获取日期
interface DayInfo {
today: string
tomorrow: string
dayAfterTomorrow: string
}
- 写一个获取日期的方法
/**
* 获取今天,明天,后天的日期
*/
getDays(): DayInfo {
//今天
const today = new Date();
//+1 明天
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
//+2 后天
const dayAfterTomorrow = new Date(today);
dayAfterTomorrow.setDate(dayAfterTomorrow.getDate() + 2);
//为什么要定义DayInfo的好处,直接return一个对象出去
return {
today: today.toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' }),
tomorrow: tomorrow.toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' }),
dayAfterTomorrow: dayAfterTomorrow.toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' })
}
}
- 写一个根据日期获取影片购票的时间段
注意:在这里使用本地数据模拟影片播购票的时间段
/**
* 获取影片播购票的时间段
*/
getMovieDateList(postion: number) {
this.movieDateInfoList = []
if (postion == 0) {
this.movieDateInfoList.push(new MovieDateInfo("08:45", "09:45散场", "原版3D", "1号厅", 58))
this.movieDateInfoList.push(new MovieDateInfo("09:35", "10:28散场", "原版2D", "2号厅", 68));
this.movieDateInfoList.push(new MovieDateInfo("10:05", "12:09散场", "原版3D", "3号厅", 38));
this.movieDateInfoList.push(new MovieDateInfo("13:25", "15:25散场", "原版2D", "4号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("16:45", "16:26散场", "原版3D", "5号厅", 88));
this.movieDateInfoList.push(new MovieDateInfo("19:55", "21:35散场", "原版2D", "6号厅", 48));
this.movieDateInfoList.push(new MovieDateInfo("20:45", "22:25散场", "原版2D", "7号厅", 28));
this.movieDateInfoList.push(new MovieDateInfo("21:00", "23:22散场", "原版3D", "8号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("22:15", "23:46散场", "原版2D", "9号厅", 90));
this.movieDateInfoList.push(new MovieDateInfo("10:25", "12:06散场", "原版2D", "10号厅", 108));
} else if (postion == 1) {
this.movieDateInfoList.push(new MovieDateInfo("09:35", "11:22散场", "原版2D", "1号厅", 68));
this.movieDateInfoList.push(new MovieDateInfo("10:05", "11:20散场", "原版3D", "2号厅", 38));
this.movieDateInfoList.push(new MovieDateInfo("13:25", "15:24散场", "原版3D", "3号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("16:45", "18:21散场", "原版2D", "4号厅", 88));
this.movieDateInfoList.push(new MovieDateInfo("21:00", "22:16散场", "原版3D", "5号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("22:15", "23:38散场", "原版2D", "6号厅", 90));
this.movieDateInfoList.push(new MovieDateInfo("10:25", "13:56散场", "原版2D", "7号厅", 108));
} else {
this.movieDateInfoList.push(new MovieDateInfo("13:25", "15:25散场", "原版3D", "1号厅", 58));
this.movieDateInfoList.push(new MovieDateInfo("16:45", "18:23散场", "原版2D", "2号厅", 48));
this.movieDateInfoList.push(new MovieDateInfo("20:45", "21:22散场", "原版3D", "3号厅", 28));
this.movieDateInfoList.push(new MovieDateInfo("21:00", "22:25散场", "原版2D", "4号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("10:25", "12:29散场", "原版3D", "5号厅", 108));
}
}
- 数据MovieDateInfo实体
项目->ets->新建entity文件夹 ->新建MovieDateInfo.ets类
export class MovieDateInfo {
time: string = ''
end_time: string = ''
type: string = ''
few_number: string = ''
price: number = 0
constructor(time: string, end_time: string, type: string, few_number: string, price: number) {
this.time = time
this.end_time = end_time
this.type = type
this.few_number = few_number
this.price = price
}
}
- 综合代码实现过程
import { router } from '@kit.ArkUI'
import { MovieDateInfo } from '../entity/MovieDateInfo'
/**
* @Extend装饰器 和@Styles不同,@Extend仅支持在全局定义,不支持在组件内部定义
*/
@Extend(Text)
function fanCurrentTextStatus() {
.width(80)
.height(32)
.borderRadius(20)
.fontSize(13)
.textAlign(TextAlign.Center)
}
interface DayInfo {
today: string
tomorrow: string
dayAfterTomorrow: string
}
@Entry
@Component
struct SelectMovieDatePage {
@State currentIndex: number = 0
@State movieDateInfoList: Array<MovieDateInfo> = []
setSelectedCurrentIndex(index: number) {
this.currentIndex = index
}
/**
* 获取影片播购票的时间段
*/
getMovieDateList(postion: number) {
this.movieDateInfoList = []
if (postion == 0) {
this.movieDateInfoList.push(new MovieDateInfo("08:45", "09:45散场", "原版3D", "1号厅", 58))
this.movieDateInfoList.push(new MovieDateInfo("09:35", "10:28散场", "原版2D", "2号厅", 68));
this.movieDateInfoList.push(new MovieDateInfo("10:05", "12:09散场", "原版3D", "3号厅", 38));
this.movieDateInfoList.push(new MovieDateInfo("13:25", "15:25散场", "原版2D", "4号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("16:45", "16:26散场", "原版3D", "5号厅", 88));
this.movieDateInfoList.push(new MovieDateInfo("19:55", "21:35散场", "原版2D", "6号厅", 48));
this.movieDateInfoList.push(new MovieDateInfo("20:45", "22:25散场", "原版2D", "7号厅", 28));
this.movieDateInfoList.push(new MovieDateInfo("21:00", "23:22散场", "原版3D", "8号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("22:15", "23:46散场", "原版2D", "9号厅", 90));
this.movieDateInfoList.push(new MovieDateInfo("10:25", "12:06散场", "原版2D", "10号厅", 108));
} else if (postion == 1) {
this.movieDateInfoList.push(new MovieDateInfo("09:35", "11:22散场", "原版2D", "1号厅", 68));
this.movieDateInfoList.push(new MovieDateInfo("10:05", "11:20散场", "原版3D", "2号厅", 38));
this.movieDateInfoList.push(new MovieDateInfo("13:25", "15:24散场", "原版3D", "3号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("16:45", "18:21散场", "原版2D", "4号厅", 88));
this.movieDateInfoList.push(new MovieDateInfo("21:00", "22:16散场", "原版3D", "5号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("22:15", "23:38散场", "原版2D", "6号厅", 90));
this.movieDateInfoList.push(new MovieDateInfo("10:25", "13:56散场", "原版2D", "7号厅", 108));
} else {
this.movieDateInfoList.push(new MovieDateInfo("13:25", "15:25散场", "原版3D", "1号厅", 58));
this.movieDateInfoList.push(new MovieDateInfo("16:45", "18:23散场", "原版2D", "2号厅", 48));
this.movieDateInfoList.push(new MovieDateInfo("20:45", "21:22散场", "原版3D", "3号厅", 28));
this.movieDateInfoList.push(new MovieDateInfo("21:00", "22:25散场", "原版2D", "4号厅", 78));
this.movieDateInfoList.push(new MovieDateInfo("10:25", "12:29散场", "原版3D", "5号厅", 108));
}
}
/**
* 获取今天,明天,后天的日期
*/
getDays(): DayInfo {
//今天
const today = new Date();
//+1 明天
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
//+2 后天
const dayAfterTomorrow = new Date(today);
dayAfterTomorrow.setDate(dayAfterTomorrow.getDate() + 2);
//为什么要定义DayInfo的好处,直接return一个对象出去
return {
today: today.toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' }),
tomorrow: tomorrow.toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' }),
dayAfterTomorrow: dayAfterTomorrow.toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' })
}
}
aboutToAppear(): void {
this.getMovieDateList(this.currentIndex)
}
build() {
Column() {
//标题栏
Row() {
Image($r('app.media.img_back')).width(26).onClick(() => {
router.back()
})
//标题栏
Text('上影国际影城(新业坊店)')
.fontSize(21)
.fontWeight(600)
}
.width('100%')
//选择日日期栏
Row() {
Text(`今天${this.getDays().today}`)
.fanCurrentTextStatus()
.backgroundColor(this.currentIndex == 0 ? '#e22418' : '#f5f5f5')
.fontColor(this.currentIndex == 0 ? Color.White : Color.Black)
.onClick(() => {
this.setSelectedCurrentIndex(0)
this.getMovieDateList(this.currentIndex)
})
Text(`明天${this.getDays().tomorrow}`)
.fanCurrentTextStatus()
.backgroundColor(this.currentIndex == 1 ? '#e22418' : '#f5f5f5')
.fontColor(this.currentIndex == 1 ? Color.White : Color.Black)
.margin({ left: 10, right: 10 })
.onClick(() => {
this.setSelectedCurrentIndex(1)
this.getMovieDateList(this.currentIndex)
})
Text(`后天${this.getDays().dayAfterTomorrow}`)
.fanCurrentTextStatus()
.backgroundColor(this.currentIndex == 2 ? '#e22418' : '#f5f5f5')
.fontColor(this.currentIndex == 2 ? Color.White : Color.Black)
.onClick(() => {
this.setSelectedCurrentIndex(2)
this.getMovieDateList(this.currentIndex)
})
}.width('100%').padding(10).margin(16)
List({ space: 16 }) {
ForEach(this.movieDateInfoList, (item: MovieDateInfo) => {
ListItem() {
Row() {
//左边结构
Row() {
Column() {
Text(item.time).fontSize(13).fontColor('#333333')
Text(item.end_time).margin({ top: 10 }).fontSize(10).fontColor('#999999')
}.alignItems(HorizontalAlign.Start).margin({ left: 10 })
Column() {
Text(item.type).fontSize(11).fontColor('#333333')
Text(item.few_number).margin({ top: 10 }).fontSize(10).fontColor('#999999')
}.alignItems(HorizontalAlign.Start).margin({ left: 30 })
}
//右边结构
Row() {
Row() {
Text(`¥${item.price}`).fontColor('#FF6D00').fontSize(15)
}
Column() {
Text('购票')
.backgroundColor('#e22418')
.textAlign(TextAlign.Center)
.padding({
left: 10,
right: 10,
top: 3,
bottom: 3
})
.fontColor(Color.White)
.fontSize(12)
.borderRadius(4)
}.margin({ left: 50, right: 10 })
}
}.width('100%').justifyContent(FlexAlign.SpaceBetween)
}
})
}
}
.height('100%')
.width('100%')
}
}
注意事项:在使用ForEach渲染列表的时候,ListItem只能在循环里面