当前位置: 首页 > news >正文

鸿蒙开发城市联动选择弹框

鸿蒙开发城市联动选择弹框

城市联动选择弹框不容易,在Android那边也是不容易。选择某个省份时,城市要对得上,切换得及时

一、思路:

关键用@Provide和@Consume互相监听对方的变化

二、效果图:

在这里插入图片描述

三、视频效果:

鸿蒙Next开发教程实战案例源码分享-城市联动选择弹框

四、关键代码如下:
// 联系:893151960
@Component
export struct ProvinceLevelComponent {@State labelList: string[] = [];@Consume selectProvinceIndex: number ;@Consume currentFirst: string;@Consume dataSource: Array<ProvinceListBean>;aboutToAppear() {for (let i = 0; i < this.dataSource.length; i++) {this.labelList.push(this.dataSource[i].province)if (this.dataSource[i].province === this.currentFirst) {this.selectProvinceIndex = i;}}this.currentFirst = this.dataSource[this.selectProvinceIndex].province}build() {Column() {Column() {if (this.labelList.length === 0) {Text('暂无数据').fontSize(20)} else {TextPicker({ range: this.labelList, selected: this.selectProvinceIndex }).onChange((value: string|string[], index: number|number[]) => {if (typeof  index === 'number') {this.selectProvinceIndex = indexthis.currentFirst = this.dataSource[index].province}}).selectedTextStyle({color:$r('app.color.color_main')}).canLoop(false)}}.backgroundColor(Color.White).border({ color: '#e2e2e2', width: { right: 0.5 }}).width('100%').layoutWeight(1).justifyContent(FlexAlign.Center)}.height('100%')}
}
@Component
export struct CityLevelComponent {@State mTip: string = '暂无数据'@Consume @Watch('onFirstChange') currentFirst: string;@Consume currentSecond: string;@Consume selectProvinceIndex: number;@State labelList: string[] = [];@State select: number = 0;@Consume dataSource: Array<ProvinceListBean>;isIncludeUnlimited:boolean = trueaboutToAppear(){this.onFirstChange()}build() {Column() {Column() {if (this.labelList.length === 0) {Text(this.mTip).fontSize(20)} else {TextPicker({ range: this.labelList, selected: this.select }).onChange((value: string | string[], index: number | number[]) => {if (typeof  index === 'number') {this.select = indexthis.currentSecond = this.labelList[index]}}).selectedTextStyle({color:$r('app.color.color_main')}).canLoop(false)}}.backgroundColor(Color.White).border({color: '#e2e2e2',width: { right: 0.5 }}).width('100%').layoutWeight(1).justifyContent(FlexAlign.Center)}.height('100%')}//   export interface ProvinceCityAreaDataBean{
//   version?:number
//   list?:Array<ProvinceListBean>
// }
//
// export interface ProvinceListBean{
//   province?:string
//   cityList?:Array<CityListBean>
// }
//
// export interface CityListBean{
//   city?:string
// }onFirstChange() {this.labelList = []let tempList : string[] = []let cityList : Array<CityListBean> = []let json: ProvinceListBean = JSON.parse(JSON.stringify(this.dataSource[this.selectProvinceIndex]));cityList = json.cityList!if (this.isIncludeUnlimited){if (cityList.length > 1) {tempList.push('全省')}}for (let i = 0; i < cityList.length; i++) {tempList.push(cityList[i].city)}this.labelList = tempListif (this.isIncludeUnlimited || !this.currentSecond) {this.select = 0;this.currentSecond = this.labelList[this.select]} else {for (let i = 0; i < this.labelList.length; i++) {if (this.labelList[i] === this.currentSecond) {this.select = i;this.currentSecond = this.labelList[this.select]break}}// 切换了省,但是没滑动市if (this.select === 0) {this.currentSecond = this.labelList[this.select]}}}}
五、完整项目demo源码结构图:

在这里插入图片描述有需要完整源码的可私信我

http://www.lryc.cn/news/441796.html

相关文章:

  • css 控制虚线刻度尺寸
  • NLP三天入门大模型,我领先你好几个版本了
  • 专题六_模拟_算法详细总结
  • ArrayList的扩容机制
  • 一、编译原理(引论)
  • 【Javascript修炼篇】JS中的函数式编程
  • spring cxf 常用注解
  • python | x-y 网格切片
  • 【C#】vs2022 .net8
  • 【华为杯】第二十一届中国研究生数学建模竞赛
  • 首次开机android.intent.action.BOOT_COMPLETED开机广播发送慢的问题
  • 通信工程学习:什么是OLT光线路终端
  • Unity的Button组件进行扩展
  • 前端vue-插值表达式和v-html的区别
  • 【开发心得】筑梦上海:项目风云录(4)
  • el-table使用el-switch选择器没效果
  • libserailport交叉编译适配说明
  • C语言中的一些小知识(二)
  • 使用 Go 语言实现简单聊天系统
  • 用友U8二次开发工具KK-FULL-*****-EFWeb使用方法
  • 【经验帖】脏读和不可重复读的概念及影响
  • MTK zephyr平台:USB升级、枚举流程
  • golang操作mysql利器-gorm
  • 09 Shell Scriptfor循环结构语句
  • 【Java】并发集合
  • 活动邀请|景联文科技与您相约华为全联接大会2024
  • 周边游|基于springBoot的周边游平台设计与实现(附项目源码+论文+数据库)
  • 【编程基础知识】mysql是怎样执行一条sql语句的,涉及到哪些环节步骤是,mysql的整体体系结构是啥样的,有哪些组件
  • 如何上传tauri项目到csdn gitcode
  • 【速成Redis】02 Redis 五大基本数据类型常用命令