HarmonyOS 鸿蒙Next TextPickerDialog组件的三级联动

发布于 1周前 作者 phonegap100 来自 鸿蒙OS

HarmonyOS 鸿蒙Next TextPickerDialog组件的三级联动

@Entry
@Component
struct TextPickerDialogDemo1 {

@State provinceList: Province[] = [
{
“pcode”:1,
“pname”:“湖南”,
“citys”: [
{
“ccode”:11,
“cname”:“长沙”,
“areas”: [
{“acode”: 111, “aname”: “望城区” },
{“acode”: 111, “aname”: “岳麓区” },
{“acode”: 111, “aname”: “天心区” }
]
},
{
“ccode”:12,
“cname”:“湘潭”,
“areas”: [
{“acode”: 111, “aname”: “望城区” },
{“acode”: 111, “aname”: “岳麓区” },
{“acode”: 111, “aname”: “天心区” }
]
},
{
“ccode”:13,
“cname”:“株洲”,
“areas”: [
{“acode”: 111, “aname”: “望城区” },
{“acode”: 111, “aname”: “岳麓区” },
{“acode”: 111, “aname”: “天心区” }
]
}
]
},
{
“pcode”:2,
“pname”:“湖北”,
“citys”: [
{
“ccode”:21,
“cname”:“武汉”,
“areas”: [
{“acode”: 111, “aname”: “望城区” },
{“acode”: 111, “aname”: “岳麓区” },
{“acode”: 111, “aname”: “天心区” }
]
},
{
“ccode”:22,
“cname”:“荆州”,
“areas”: [
{“acode”: 111, “aname”: “望城区” },
{“acode”: 111, “aname”: “岳麓区” },
{“acode”: 111, “aname”: “天心区” }
]
}
]
},
{
“pcode”:3,
“pname”:“江西”,
“citys”: [
{
“ccode”:31,
“cname”:“萍乡”,
“areas”: [
{“acode”: 111, “aname”: “望城区” },
{“acode”: 111, “aname”: “岳麓区” },
{“acode”: 111, “aname”: “天心区” }
]
},
{
“ccode”:32,
“cname”:“武功山”,
“areas”: [
{“acode”: 111, “aname”: “望城区” },
{“acode”: 111, “aname”: “岳麓区” },
{“acode”: 111, “aname”: “天心区” }
]
},
{
“ccode”:33,
“cname”:“南昌”,
“areas”: [
{“acode”: 111, “aname”: “望城区” },
{“acode”: 111, “aname”: “岳麓区” },
{“acode”: 111, “aname”: “天心区” }
]
}
]
}
]

@State selectedProvince: number | number[] = 0;

@State provinceStringList: string[] = [];

@State cityStringList: string[] = [];

@State selectedAddress: string = ‘’;

onPageShow(): void {
for (let i = 0; i < this.provinceList.length; i++) {
this.provinceStringList.push(this.provinceList[i].pname);
}
}

build() {
Row() {
Column() {
TextInput({placeholder: “请选择收货地址”,text: this.selectedAddress})
Button(“TextPickerDialog:”)
.margin(20)
.onClick(() => {
TextPickerDialog.show({
range: this.provinceStringList,
selected: this.selectedProvince,
canLoop: false,
disappearTextStyle: {color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}},
textStyle: {color: Color.Black, font: {size: 20, weight: FontWeight.Normal}},
selectedTextStyle: {color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}},
onAccept: (value: TextPickerResult) => {
// 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
this.selectedProvince = value.index
this.selectedAddress = value.value as string;
let cityList: City[] = this.provinceList[value.index as number].citys;
this.cityStringList.splice(0,this.cityStringList.length)
for(let i = 0; i < cityList.length; i++) {
this.cityStringList.push(cityList[i].cname);
}
console.log(“this.selectedProvince的值为:”,this.selectedProvince)
TextPickerDialog.show({
range: this.cityStringList,
selected: this.selectedProvince,
canLoop: false,
disappearTextStyle: {color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}},
textStyle: {color: Color.Black, font: {size: 20, weight: FontWeight.Normal}},
selectedTextStyle: {color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}},
onAccept: (value: TextPickerResult) => {
this.selectedAddress += “/”+value.value as string;
}
})
},
onCancel: () => {
// console.info(“TextPickerDialog:onCancel()”)
},
onChange: (value: TextPickerResult) => {
// console.info(“TextPickerDialog:onChange()” + JSON.stringify(value))
}
})
})

}.width(‘100%’)
}.height(‘100%’)
}
}

class Province {
pcode: number = 0
pname: string = ‘’
citys: City[] = []
}

class City {
ccode: number = 0
cname: string = ‘’
areas: Area[] = []
}

class Area {
acode: number = 0
aname: string = ‘’
}

这个代码会报这个错误:
Type ‘{ acode: number; aname: string; }’ is missing the following properties from type ‘Area’: width, height, position, globalPosition


关于HarmonyOS 鸿蒙Next TextPickerDialog组件的三级联动的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。

4 回复

升级HarmonyOS后,发现手机的游戏性能也有了显著提升。

Area这个类和系统的重名了,换个名字就好了

有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html

Area这个应该是保留关键字吧,换个名字试试呢
回到顶部