通过UIPickerComponent动态渲染实现如下多级联动场景:
@Entry
@Component
struct UIPickerComponentExample1 {
yearList: number[] = []; // 年选择器数据
monthList: number[] = []; // 月选择器数据
dayList31: number[] = []; // 31日数据
@State dayList: number[] = []; // 日选择器数据(会根据年月进行设置)
@State yearIndex: number = 0; // 年选择器索引
@State monthIndex: number = 0; // 月选择器索引
@State dayIndex: number = 0; // 日选择器索引
@State showDay: boolean = true; // 是否展示日
@State @Watch('dateChange') dateText: string = ''; // 选中的日期信息
@State isLoop: boolean = true; // 是否循环滚动
// 日期值变化时触发
dateChange() {
console.info(this.dateText); // 打印选择器日期
}
// 当年月发生改变时修改日选择器数据
updateDayList() {
let month = this.monthList[this.monthIndex];
let year = this.yearList[this.yearIndex];
let list: number[] = [];
if (month === 1 || month === 3 || month === 5 || month === 7 || month === 8 || month === 10 || month === 12) {
list = this.dayList31; // 大月时选择器数据
} else if (month === 4 || month === 6 || month === 9 || month === 11) {
list = this.dayList31.slice(0, 30); // 小月时选择器数据
} else if ((year % 4 === 0) && (year % 100 !== 0) || (year % 400 === 0)) {
list = this.dayList31.slice(0, 29); // 闰年2月选择器数据
} else {
list = this.dayList31.slice(0, 28); // 平年2月选择器数据
}
this.dayIndex = (this.dayIndex <= list.length - 1) ? this.dayIndex : (list.length - 1); // 日超出当月时,设置为当月最后一天
this.dayList = list; // 修改选择器日选择器数据
}
// 初始化年月日列表
setList() {
for (let year = 1900; year <= 2100; year++) {
this.yearList.push(year);
}
for (let month = 1; month <= 12; month++) {
this.monthList.push(month);
}
for (let day = 1; day <= 31; day++) {
this.dayList31.push(day);
}
}
// 初始化日期
setDate(date: Date) {
this.yearIndex = this.yearList.findIndex((year) => {
return year === date.getFullYear();
});
this.monthIndex = this.monthList.findIndex((month) => {
return month === date.getMonth() + 1;
});
this.dayIndex = this.dayList31.findIndex((day) => {
return day === date.getDate();
});
}
aboutToAppear(): void {
this.setList(); // 初始化年月日的列表数据
this.setDate(new Date()); // 初始化选择器日期为当天日期
this.updateDayList(); // 更新当月的日列表
this.dateText = `${this.yearList[this.yearIndex]}年${this.monthList[this.monthIndex]}月` +
(this.showDay ? `${this.dayList[this.dayIndex]}日` : ``);
}
build() {
Column({ space: 10 }) {
Row() {
UIPickerComponent({ selectedIndex: this.yearIndex }) {
ForEach(this.yearList, (year: number) => {
Text(`${year}年`);
});
}
.onChange((selectIndex) => {
this.yearIndex = selectIndex;
this.updateDayList();
this.dateText = `${this.yearList[this.yearIndex]}年${this.monthList[this.monthIndex]}月` +
(this.showDay ? `${this.dayList[this.dayIndex]}日` : ``);
})
.selectionIndicator({ type: PickerIndicatorType.DIVIDER })
.width('25%')
.canLoop(this.isLoop); // 是否循环
UIPickerComponent({ selectedIndex: this.monthIndex }) {
ForEach(this.monthList, (month: number) => {
Text(`${month}月`);
});
}
.onChange((selectIndex) => {
this.monthIndex = selectIndex;
this.updateDayList();
this.dateText = `${this.yearList[this.yearIndex]}年${this.monthList[this.monthIndex]}月` +
(this.showDay ? `${this.dayList[this.dayIndex]}日` : ``);
})
.selectionIndicator({ type: PickerIndicatorType.DIVIDER })
.width('25%')
.canLoop(this.isLoop); // 是否循环
if (this.showDay) { // 是否展示日选择器
UIPickerComponent({ selectedIndex: this.dayIndex }) {
ForEach(this.dayList, (day: number) => {
Text(`${day}日`);
});
}
.onChange((selectIndex) => {
this.dayIndex = selectIndex;
this.dateText = `${this.yearList[this.yearIndex]}年${this.monthList[this.monthIndex]}月` +
(this.showDay ? `${this.dayList[this.dayIndex]}日` : ``);
})
.selectionIndicator({ type: PickerIndicatorType.DIVIDER })
.width('25%')
.canLoop(this.isLoop); // 是否循环
}
};
Text(this.dateText);
Button(`是否开启日选择器,当前状态:${this.showDay}`).onClick(() => {
this.showDay = !this.showDay;
this.dateText = `${this.yearList[this.yearIndex]}年${this.monthList[this.monthIndex]}月` +
(this.showDay ? `${this.dayList[this.dayIndex]}日` : ``);
});
Button(`是否循环滚动,当前状态:${this.isLoop}`).onClick(() => {
this.isLoop = !this.isLoop;
});
}
.height('100%')
.width('100%');
}
} 运行效果图如下:

@Entry
@Component
struct UIPickerComponentExample2 {
startYear: number = 2020; // 选择器开始年
startMonth: number = 4; // 选择器第一年的开始月
endYear: number = new Date().getFullYear(); // 选择器结束年
endMonth: number = new Date().getMonth() + 1; // 选择器最后一年的结束月
yearList: number[] = []; // 年选择器数据
monthList12: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
@State yearIndex: number = 0;
@State monthList: number[] = []; // 月选择器数据
@State monthIndex: number = 0;
aboutToAppear(): void {
for (let year = this.startYear; year <= this.endYear; year++) {
this.yearList.push(year);
}
this.yearIndex = this.yearList.length - 1;
this.monthList = this.monthList12.slice(0, this.endMonth);
this.monthIndex = this.monthList.length - 1;
}
build() {
Column() {
Row() {
UIPickerComponent({ selectedIndex: this.yearIndex }) {
ForEach(this.yearList, (year: number) => {
Text(`${year}年`);
});
}
.onChange((selectIndex) => {
this.monthIndex = 0; // 年变动时月索引设置为0
if (selectIndex === 0) {
this.monthList = this.monthList12.slice(this.startMonth - 1); // 第一年月列表
} else if (selectIndex === this.yearList.length - 1) {
this.monthList = this.monthList12.slice(0, this.endMonth); // 最后一年月列表
} else {
this.monthList = this.monthList12; // 正常的12个月
}
})
.selectionIndicator({ type: PickerIndicatorType.DIVIDER })
.width('25%')
.canLoop(false);
UIPickerComponent({ selectedIndex: this.monthIndex }) {
ForEach(this.monthList, (month: number) => {
Text(`${month}月`);
});
}
.selectionIndicator({ type: PickerIndicatorType.DIVIDER })
.width('25%')
.canLoop(false)
.onChange((selectIndex) => {
this.monthIndex = selectIndex;
});
};
}
.width('100%')
.height('100%');
}
} 运行效果图如下:
