ability_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="horizontal">
<Picker
ohos:id="$+id:province"
ohos:height="match_content"
ohos:width="100vp"
ohos:normal_text_size="20fp"
ohos:selected_text_size="20fp"
ohos:value="0"/>
<Picker
ohos:id="$+id:city"
ohos:height="match_content"
ohos:width="100vp"
ohos:left_margin="10vp"
ohos:normal_text_size="20fp"
ohos:selected_text_size="20fp"
ohos:value="0"/>
<Picker
ohos:id="$+id:district"
ohos:height="match_content"
ohos:width="100vp"
ohos:left_margin="10vp"
ohos:normal_text_size="20fp"
ohos:selected_text_size="20fp"
ohos:value="0"/>
</DirectionalLayout>
建一个省的javabean类,记录省份名字和包含的市
package com.example.pickerapplication.domain;
import java.util.ArrayList;
public class Province {
private String name;
private ArrayList<String> list;
public Province() {
}
public Province(String name, ArrayList<String> list) {
this.name = name;
this.list = list;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getList() {
return list;
}
public void setList(ArrayList<String> list) {
this.list = list;
}
}
MainAbilitySlice.java代码:
package com.example.pickerapplication.slice;
import com.example.pickerapplication.ResourceTable;
import com.example.pickerapplication.domain.Province;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Picker;
import java.util.ArrayList;
public class MainAbilitySlice extends AbilitySlice implements Picker.ValueChangedListener {
Picker province;
Picker city;
Picker district;
ArrayList<Province> provinceList;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
province = (Picker) findComponentById(ResourceTable.Id_province);
city = (Picker) findComponentById(ResourceTable.Id_city);
district = (Picker) findComponentById(ResourceTable.Id_district);
provinceList = getData();
province.setMaxValue(provinceList.size() - 1);
province.setFormatter(i -> provinceList.get(i).getName());
city.setMaxValue(provinceList.get(0).getList().size() - 1);
city.setFormatter(i -> provinceList.get(0).getList().get(i));
province.setValueChangedListener(this);
}
@Override
public void onValueChanged(Picker picker, int oldValue, int newValue) {
if (picker == province) {
Province chooseProvince = provinceList.get(newValue);
city.setMaxValue(chooseProvince.getList().size() - 1);
city.setFormatter(i -> chooseProvince.getList().get(i));
city.setValue(0);
}
}
public ArrayList<Province> getData() {
ArrayList<Province> provinceList = new ArrayList<>();
ArrayList<String> jiangSuCitys = new ArrayList<>();
jiangSuCitys.add("南京市");
jiangSuCitys.add("苏州市");
jiangSuCitys.add("无锡市");
jiangSuCitys.add("扬州市");
jiangSuCitys.add("徐州市");
ArrayList<String> zheJiangCitys = new ArrayList<>();
zheJiangCitys.add("杭州市");
zheJiangCitys.add("宁波市");
zheJiangCitys.add("温州市");
zheJiangCitys.add("绍兴市");
zheJiangCitys.add("嘉兴市");
zheJiangCitys.add("舟山市");
ArrayList<String> fuJianCitys = new ArrayList<>();
fuJianCitys.add("福州市");
fuJianCitys.add("厦门市");
fuJianCitys.add("泉州市");
fuJianCitys.add("莆田市");
fuJianCitys.add("宁德市");
fuJianCitys.add("龙岩市");
provinceList.add(new Province("江苏省", jiangSuCitys));
provinceList.add(new Province("浙江省", zheJiangCitys));
provinceList.add(new Province("福建省", fuJianCitys));
return provinceList;
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
结果演示:
省市区三级联动只要再把区嵌套到市里面即可,思路一样
|