这篇文章将为大家详细讲解有关怎么在Android中使用PickerView实现一个三级联动效果,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
龙港网站建设公司创新互联公司,龙港网站设计制作,有大型网站制作公司丰富经验。已为龙港1000多家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的龙港做网站的公司定做!
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
(1)在app目录下新建一个assets文件夹,将province_data.xml文件复制到该文件夹下,该XML文件里保存了全国的省市县的地址数据。
(2)使用SAX解析xml文件中的数据,并将数据填充到PickerView这个控件中。
—ProvinceModel—
public class ProvinceModel implements IPickerViewData { private String name; private List<CityModel> cityList; public ProvinceModel() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<CityModel> getCityList() { return cityList; } public void setCityList(List<CityModel> cityList) { this.cityList = cityList; } @Override public String getPickerViewText() { return name; } }
—CityModel—
public class CityModel implements IPickerViewData { private String name; private List<DistrictModel> districtList; public CityModel() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<DistrictModel> getDistrictList() { return districtList; } public void setDistrictList(List<DistrictModel> districtList) { this.districtList = districtList; } @Override public String toString() { return name; } @Override public String getPickerViewText() { return name; } }
—DistrictModel—
public class DistrictModel implements IPickerViewData { private String name; private String zipcode; public DistrictModel() { super(); } public String getZipcode() { return zipcode; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } @Override public String getPickerViewText() { return name; } }
—XmlParserHandler—
public class XmlParserHandler extends DefaultHandler { /** * 存储所有的解析对象 */ private List<ProvinceModel> provinceList = new ArrayList<>(); private ProvinceModel provinceModel = new ProvinceModel(); private CityModel cityModel = new CityModel(); private DistrictModel districtModel = new DistrictModel(); public XmlParserHandler(){} public List<ProvinceModel> getDataList() { return provinceList; } @Override public void startDocument() throws SAXException { super.startDocument(); } @Override public void endDocument() throws SAXException { super.endDocument(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equals("province")) { provinceModel = new ProvinceModel(); provinceModel.setName(attributes.getValue(0)); provinceModel.setCityList(new ArrayList<CityModel>()); } else if (qName.equals("city")) { cityModel = new CityModel(); cityModel.setName(attributes.getValue(0)); cityModel.setDistrictList(new ArrayList<DistrictModel>()); } else if (qName.equals("district")) { districtModel = new DistrictModel(); districtModel.setName(attributes.getValue(0)); districtModel.setZipcode(attributes.getValue(1)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if(qName.equals("district")) { cityModel.getDistrictList().add(districtModel); } else if(qName.equals("city")) { provinceModel.getCityList().add(cityModel); } else if(qName.equals("province")) { provinceList.add(provinceModel); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); } }
public class AddressAddActivity extends BaseActivity { private OptionsPickerView mCityPickerView; private ArrayList<ProvinceModel> mProvinces = new ArrayList<>(); private ArrayList<ArrayList<CityModel>> mCities = new ArrayList<>(); private ArrayList<ArrayList<ArrayList<DistrictModel>>> mDistricts = new ArrayList<>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_address_add); ViewUtils.inject(this); init(); } private void init() { initProvinceDatas(); try { //选项选择器 mCityPickerView = new OptionsPickerView(this); mCityPickerView.setPicker( mProvinces, mCities, mDistricts, true); mCityPickerView.setTitle("选择城市"); mCityPickerView.setCyclic(false, false , false); mCityPickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() { @Override public void onOptionsSelect(int options1, int option2, int options3) { String address = mProvinces.get(options1).getName() + " " + mCities.get(options1).get(option2) + " " + mDistricts.get(options1).get(option2).get(options3); mTxtAddress.setText(address); } }); } catch (Exception e) { e.printStackTrace(); } } @OnClick(R.id.ll_city_picker) public void showpickerView(View view) { mCityPickerView.show(); } protected void initProvinceDatas() { AssetManager asset = getAssets(); try { InputStream input = asset.open("province_data.xml"); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = spf.newSAXParser(); XmlParserHandler handler = new XmlParserHandler(); parser.parse(input , handler); input.close(); //获取解析出来的数据 mProvinces = (ArrayList<ProvinceModel>) handler.getDataList(); } catch (Throwable e) { e.printStackTrace(); } if (mProvinces != null) { for (ProvinceModel p : mProvinces) { List<CityModel> cities = p.getCityList(); //组装城市数据 mCities.add((ArrayList<CityModel>) cities); ArrayList<ArrayList<DistrictModel>> dts = new ArrayList<>(); for (CityModel c : cities) { List<DistrictModel> districts = c.getDistrictList(); dts.add((ArrayList<DistrictModel>) districts); } mDistricts.add(dts); } } } }
关于怎么在Android中使用PickerView实现一个三级联动效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
标题名称:怎么在Android中使用PickerView实现一个三级联动效果
转载来于:https://www.cdcxhl.com/article24/gjpsce.html
成都网站建设公司_创新互联,为您提供网站导航、企业建站、静态网站、建站公司、微信公众号、面包屑导航
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联