方案二:可通过TextPicker的多列模式自定义年月选择器。- 使用range属性定义两列数据(第一列为年份,第二列为月份)。
- 结合onChange监听选择结果的变化。
@Entry
@Component
struct TextPickerExample {
private years: string[] =
['1990年', '1991年', '1992年', '1993年', '1994年', '1995年', '1996年', '1997年', '1998年', '1999年',
'2000年', '2001年', '2002年', '2003年', '2004年', '2005年', '2006年', '2007年', '2008年', '2009年', '2010年',
'2011年', '2012年',
'2013年', '2014年', '2015年', '2016年', '2017年', '2018年', '2019年', '2020年', '2021年', '2022年', '2023年',
'2024年', '2025年',
'2026年', '2027年', '2028年', '2029年', '2030年'];
private months: string[] = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
private multi: string[][] = [this.years, this.months];
build() {
Column() {
TextPicker({ range: this.multi })
.onChange((value: string | string[], index: number | number[]) => {
console.info(`TextPicker 多列:onChange ${value}, index: ${index}`);
}).margin(50)
}
.width('100%')
.height('100%')
}
}