這篇文章將為大家詳細(xì)講解有關(guān)怎么在Android中使用PickerView實(shí)現(xiàn)一個三級聯(lián)動效果,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
龍港網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,龍港網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為龍港1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的龍港做網(wǎng)站的公司定做!
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
(1)在app目錄下新建一個assets文件夾,將province_data.xml文件復(fù)制到該文件夾下,該XML文件里保存了全國的省市縣的地址數(shù)據(jù)。
(2)使用SAX解析xml文件中的數(shù)據(jù),并將數(shù)據(jù)填充到PickerView這個控件中。
—ProvinceModel—
public class ProvinceModel implements IPickerViewData { private String name; private ListcityList; public ProvinceModel() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getCityList() { return cityList; } public void setCityList(List cityList) { this.cityList = cityList; } @Override public String getPickerViewText() { return name; } }
—CityModel—
public class CityModel implements IPickerViewData { private String name; private ListdistrictList; public CityModel() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getDistrictList() { return districtList; } public void setDistrictList(List 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 ListprovinceList = new ArrayList<>(); private ProvinceModel provinceModel = new ProvinceModel(); private CityModel cityModel = new CityModel(); private DistrictModel districtModel = new DistrictModel(); public XmlParserHandler(){} public List 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 ()); } else if (qName.equals("city")) { cityModel = new CityModel(); cityModel.setName(attributes.getValue(0)); cityModel.setDistrictList(new ArrayList ()); } 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 ArrayListmProvinces = new ArrayList<>(); private ArrayList > mCities = new ArrayList<>(); private ArrayList >> 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 { //選項(xiàng)選擇器 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(); //獲取解析出來的數(shù)據(jù) mProvinces = (ArrayList ) handler.getDataList(); } catch (Throwable e) { e.printStackTrace(); } if (mProvinces != null) { for (ProvinceModel p : mProvinces) { List cities = p.getCityList(); //組裝城市數(shù)據(jù) mCities.add((ArrayList ) cities); ArrayList > dts = new ArrayList<>(); for (CityModel c : cities) { List districts = c.getDistrictList(); dts.add((ArrayList ) districts); } mDistricts.add(dts); } } } }
關(guān)于怎么在Android中使用PickerView實(shí)現(xiàn)一個三級聯(lián)動效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。