HarmonyOS ArkUI Select 与 TextPicker 选择器:下拉、滚筒与省市联动
系列鸿蒙 HarmonyOS 6.1 新特性实战 · 第 38 篇Select下拉选择器和 TextPicker滚筒选择器是两种互补的选择组件Select 适合选项多、界面紧凑的场景TextPicker 适合连续值或较少选项的滚动选择。本篇还演示了用两个 Select 联动实现省市级联的实用模式。运行效果初始状态展示 Select 下拉和 TextPicker 滚筒点击城市 Select 展开下拉列表一、Select 下拉选择器private cities: SelectOption[] [ { value: 北京 }, { value: 上海 }, { value: 广州 }, { value: 深圳 }, { value: 成都 }, { value: 杭州 } ] State selectedCity: number 0 Select(this.cities) .selected(this.selectedCity) .value(this.cities[this.selectedCity].value as string) // 显示当前选中项 .font({ size: 15 }) .fontColor(#333) .selectedOptionBgColor(#e8f0ff) // 已选项背景色 .selectedOptionFontColor(#0066ff) // 已选项文字色 .onSelect((idx: number) { this.selectedCity idx })注意属性名是selectedOptionBgColor不是selectedOptionBackgroundColorselectedOptionFontColor也没有驼峰缩写问题但实测optionBgColor不存在可以在 DevEco 中.触发自动补全确认可用属性。二、TextPicker 滚筒选择器private hours: string[] [00, 06, 08, 09, 10, 12, 14, 16, 18, 20, 22, 23] State timePickerIdx: number 1 State textPickerVal: string TextPicker({ range: this.hours, selected: this.timePickerIdx }) .onChange((val: string | string[], idx: number | number[]) { if (typeof val string) { this.textPickerVal val this.timePickerIdx typeof idx number ? idx : 0 } }) Text(选中时刻 (this.textPickerVal.length 0 ? this.textPickerVal : this.hours[this.timePickerIdx]) :00) .fontSize(16).fontColor(#0066ff).fontWeight(FontWeight.Bold)回调类型注意onChange的参数类型是string | string[]联合类型单列选择时是string需要类型判断后使用。三、省市联动Select 级联用两个 Select 联动实现省市两级选择private provinces: string[] [广东省, 浙江省, 四川省] private provinceCities: string[][] [ [广州市, 深圳市, 佛山市, 东莞市], [杭州市, 宁波市, 温州市], [成都市, 绵阳市, 德阳市] ] State cascadeProvince: number 0 State cascadeCity: number 0 Row({ space: 8 }) { // 省份 Select Select(this.provinces.map((p: string): SelectOption ({ value: p }))) .selected(this.cascadeProvince) .value(this.provinces[this.cascadeProvince]) .font({ size: 14 }).layoutWeight(1) .onSelect((idx: number) { this.cascadeProvince idx this.cascadeCity 0 // 切换省份时重置城市 }) // 城市 Select根据省份动态变化 Select(this.provinceCities[this.cascadeProvince].map((c: string): SelectOption ({ value: c }))) .selected(this.cascadeCity) .value(this.provinceCities[this.cascadeProvince][this.cascadeCity]) .font({ size: 14 }).layoutWeight(1) .onSelect((idx: number) { this.cascadeCity idx }) }关键点城市 Select 的数据源this.provinceCities[this.cascadeProvince]是响应式的省份变化时自动更新城市列表。完整代码Entry Component struct Index { State selectedCity: number 0 State selectedFruit: number 0 State textPickerVal: string State timePickerIdx: number 1 State cascadeProvince: number 0 State cascadeCity: number 0 private cities: SelectOption[] [ { value: 北京 }, { value: 上海 }, { value: 广州 }, { value: 深圳 }, { value: 成都 }, { value: 杭州 } ] private fruits: SelectOption[] [ { value: 苹果 }, { value: 香蕉 }, { value: 橙子 }, { value: 葡萄 }, { value: 草莓 } ] private hours: string[] [00, 06, 08, 09, 10, 12, 14, 16, 18, 20, 22, 23] private provinces: string[] [广东省, 浙江省, 四川省] private provinceCities: string[][] [ [广州市, 深圳市, 佛山市, 东莞市], [杭州市, 宁波市, 温州市], [成都市, 绵阳市, 德阳市] ] build() { Scroll() { Column({ space: 20 }) { Text(Select 与 TextPicker 选择器) .fontSize(22).fontWeight(FontWeight.Bold).fontColor(#1a1a1a) // Select Column({ space: 10 }) { Text(一、Select 下拉选择器).fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) Row({ space: 12 }) { Text(城市).fontSize(14).fontColor(#555).width(50) Select(this.cities) .selected(this.selectedCity) .value(this.cities[this.selectedCity].value as string) .font({ size: 15 }).fontColor(#333) .selectedOptionBgColor(#e8f0ff).selectedOptionFontColor(#0066ff) .onSelect((idx: number) { this.selectedCity idx }) .layoutWeight(1) } .width(100%).alignItems(VerticalAlign.Center) Text(已选${this.cities[this.selectedCity].value}) .fontSize(14).fontColor(#0066ff).fontWeight(FontWeight.Bold) } .padding(12).backgroundColor(#fff).borderRadius(8).width(100%) .border({ width: 1, color: #e0e0e0 }).alignItems(HorizontalAlign.Start) // TextPicker Column({ space: 10 }) { Text(二、TextPicker 滚筒选择器).fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) TextPicker({ range: this.hours, selected: this.timePickerIdx }) .onChange((val: string | string[], idx: number | number[]) { if (typeof val string) { this.textPickerVal val this.timePickerIdx typeof idx number ? idx : 0 } }) Text(选中时刻 (this.textPickerVal.length 0 ? this.textPickerVal : this.hours[this.timePickerIdx]) :00) .fontSize(16).fontColor(#0066ff).fontWeight(FontWeight.Bold) } .padding(12).backgroundColor(#fff).borderRadius(8).width(100%) .border({ width: 1, color: #e0e0e0 }).alignItems(HorizontalAlign.Start) // 省市联动 Column({ space: 10 }) { Text(三、省市联动Select 级联).fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) Row({ space: 8 }) { Select(this.provinces.map((p: string): SelectOption ({ value: p }))) .selected(this.cascadeProvince).value(this.provinces[this.cascadeProvince]) .font({ size: 14 }).layoutWeight(1) .onSelect((idx: number) { this.cascadeProvince idx; this.cascadeCity 0 }) Select(this.provinceCities[this.cascadeProvince].map((c: string): SelectOption ({ value: c }))) .selected(this.cascadeCity) .value(this.provinceCities[this.cascadeProvince][this.cascadeCity]) .font({ size: 14 }).layoutWeight(1) .onSelect((idx: number) { this.cascadeCity idx }) } .width(100%).alignItems(VerticalAlign.Center) Text(选中${this.provinces[this.cascadeProvince]} ${this.provinceCities[this.cascadeProvince][this.cascadeCity]}) .fontSize(14).fontColor(#0066ff).fontWeight(FontWeight.Bold) } .padding(12).backgroundColor(#fff).borderRadius(8).width(100%) .border({ width: 1, color: #e0e0e0 }).alignItems(HorizontalAlign.Start) } .padding(20).width(100%) } .width(100%).height(100%).backgroundColor(#f5f5f5) } }API 速查组件/属性说明Select(options: SelectOption[])下拉选择options 为{value: string}[].selected(idx)初始选中索引.value(显示文字)未展开时显示的文本.selectedOptionBgColor(#e8f0ff)已选项背景色.selectedOptionFontColor(#0066ff)已选项文字色.onSelect((idx: number) {})选中回调TextPicker({ range: string[], selected: idx })滚筒选择.onChange((val, idx) {})滚动变化回调val 是string | string[]小结属性名要精确selectedOptionBgColor而非selectedOptionBackgroundColor用 DevEco 自动补全确认TextPicker onChange 处理联合类型typeof val string判断后再使用级联靠响应式数据城市 Select 的 range 直接用this.provinceCities[province]省份变化自动刷新切换父级别时重置子级索引this.cascadeCity 0防止越界上一篇DatePicker / TimePicker 完全指南 | 下一篇Badge 徽标与 Chip 标签组件

相关新闻

日志系统的设计:从 console.log 到结构化日志

日志系统的设计:从 console.log 到结构化日志

日志系统的设计:从 console.log 到结构化日志 一、console.log 在什么时候不够用 开发阶段,console.log 是最直接的调试工具。打印请求参数、输出中间结果,在本地跑一下就知道发生了什么。这个习惯在部署到生产环境后,往往延续下来——生产代码里依然充斥着 console…

2026/11/7 2:39:55 阅读更多 →
AI外贸获客系统:企业如何利用AI提升海外客户开发效率?

AI外贸获客系统:企业如何利用AI提升海外客户开发效率?

随着全球贸易环境不断变化,外贸企业正在寻找更加高效的客户开发方式。过去,外贸企业主要依靠人工完成客户开发流程。销售人员寻找目标市场。销售人员收集客户资料。销售人员联系潜在客户。销售人员持续跟进合作机会。这种方式长期以来发挥了重要作用。但…

2026/11/13 16:57:09 阅读更多 →
终极指南:3个核心技巧免费解锁Wand专业版完整功能

终极指南:3个核心技巧免费解锁Wand专业版完整功能

终极指南:3个核心技巧免费解锁Wand专业版完整功能 【免费下载链接】Wand-Enhancer Advanced UX and interoperability extension for Wand (WeMod) app 项目地址: https://gitcode.com/GitHub_Trending/we/Wand-Enhancer 你是否曾因游戏修改工具的功能限制而…

2026/11/10 6:07:48 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/19 21:38:27 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/19 21:38:49 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/19 21:39:09 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/19 21:36:50 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/19 21:37:45 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/19 21:36:20 阅读更多 →

日新闻

周新闻

月新闻