本文主要是介绍android-wheel三级联动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在github上面有一个叫做 android-wheel 的开源控件, 代码地址https://github.com/maarek/android-wheel
源码下载地址。。。。
主界面布局
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#E9E9E9"android:orientation="vertical" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><kankan.wheel.widget.WheelViewandroid:id="@+id/id_province"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1" ></kankan.wheel.widget.WheelView><kankan.wheel.widget.WheelViewandroid:id="@+id/id_city"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1" ></kankan.wheel.widget.WheelView><kankan.wheel.widget.WheelViewandroid:id="@+id/id_district"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1" ></kankan.wheel.widget.WheelView></LinearLayout><Buttonandroid:id="@+id/btn_confirm"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dip"android:gravity="center"android:text="确定"android:textColor="#000000" /></LinearLayout>
assets资源文件下--province_data.xml(一部分)
<root><province name="安徽省"><city name="安庆市"><district name="枞阳县" zipcode="246000" /><district name="大观区" zipcode="246000" /><district name="怀宁县" zipcode="246000" /><district name="潜山县" zipcode="246000" /><district name="宿松县" zipcode="246000" /><district name="太湖县" zipcode="246000" /><district name="桐城市" zipcode="246000" /><district name="望江县" zipcode="246000" /><district name="宜秀区" zipcode="246000" /><district name="迎江区" zipcode="246000" /><district name="岳西县" zipcode="246000" /><district name="其他" zipcode="246000" /></city>
先看省份、城市、县乡的bean类
ProvinceModel
package com.mrwujay.cascade.model;import java.util.List;public class ProvinceModel {private String name;private List<CityModel> cityList;public ProvinceModel() {super();}public ProvinceModel(String name, List<CityModel> cityList) {super();this.name = name;this.cityList = cityList;}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;}@Overridepublic String toString() {return "ProvinceModel [name=" + name + ", cityList=" + cityList + "]";}}
package com.mrwujay.cascade.model;import java.util.List;public class CityModel {private String name;private List<DistrictModel> districtList;public CityModel() {super();}public CityModel(String name, List<DistrictModel> districtList) {super();this.name = name;this.districtList = districtList;}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;}@Overridepublic String toString() {return "CityModel [name=" + name + ", districtList=" + districtList+ "]";}}
DistrictModel
package com.mrwujay.cascade.model;public class DistrictModel {private String name;private String zipcode;public DistrictModel() {super();}public DistrictModel(String name, String zipcode) {super();this.name = name;this.zipcode = zipcode;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getZipcode() {return zipcode;}public void setZipcode(String zipcode) {this.zipcode = zipcode;}@Overridepublic String toString() {return "DistrictModel [name=" + name + ", zipcode=" + zipcode + "]";}}
看自定义的XmlParserHandler
package com.mrwujay.cascade.service;import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;import com.mrwujay.cascade.model.CityModel;
import com.mrwujay.cascade.model.DistrictModel;
import com.mrwujay.cascade.model.ProvinceModel;public class XmlParserHandler extends DefaultHandler {/*** 存储所有的解析对象*/private List<ProvinceModel> provinceList = new ArrayList<ProvinceModel>();public XmlParserHandler() {}/*** 对外暴露的方法*/public List<ProvinceModel> getDataList() {return provinceList;}@Overridepublic void startDocument() throws SAXException {// 当读到第一个开始标签的时候,会触发这个方法}ProvinceModel provinceModel = new ProvinceModel();CityModel cityModel = new CityModel();DistrictModel districtModel = new DistrictModel();
/*** <province name="安徽省"><city name="安庆市"><district name="枞阳县" zipcode="246000" /><district name="大观区" zipcode="246000" /><district name="怀宁县" zipcode="246000" /><district name="潜山县" zipcode="246000" /><district name="宿松县" zipcode="246000" /><district name="太湖县" zipcode="246000" /><district name="桐城市" zipcode="246000" /><district name="望江县" zipcode="246000" /><district name="宜秀区" zipcode="246000" /><district name="迎江区" zipcode="246000" /><district name="岳西县" zipcode="246000" /><district name="其他" zipcode="246000" /></city>*/@Overridepublic 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));}}@Overridepublic 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);}}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {}}
接下来看基类BaseActivity
package com.mrwujay.cascade.activity;import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import android.app.Activity;
import android.content.res.AssetManager;import com.mrwujay.cascade.model.CityModel;
import com.mrwujay.cascade.model.DistrictModel;
import com.mrwujay.cascade.model.ProvinceModel;
import com.mrwujay.cascade.service.XmlParserHandler;public class BaseActivity extends Activity {/*** 所有省*/protected String[] mProvinceDatas;/*** key - 省 value - 市*/protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();/*** key - 市 values - 区*/protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();/*** key - 区 values - 邮编*/protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();/*** 当前省的名称*/protected String mCurrentProviceName;/*** 当前市的名称*/protected String mCurrentCityName;/*** 当前区的名称*/protected String mCurrentDistrictName = "";/*** 当前区的邮政编码*/protected String mCurrentZipCode = "";/*** 解析省市区的XML数据*/protected void initProvinceDatas() {//省份集合列表List<ProvinceModel> provinceList = null;//获取资源AssetManager asset = getAssets();try {//获取输入流InputStream input = asset.open("province_data.xml");// 创建一个解析xml的工厂对象SAXParserFactory spf = SAXParserFactory.newInstance();// 解析xmlSAXParser parser = spf.newSAXParser();//解析工具XmlParserHandler handler = new XmlParserHandler();//进行解析parser.parse(input, handler);input.close();// 获取解析出来的数据provinceList = handler.getDataList();// */ 初始化默认选中的省、市、区if (provinceList != null && !provinceList.isEmpty()) {//获取第一个省份mCurrentProviceName = provinceList.get(0).getName();List<CityModel> cityList = provinceList.get(0).getCityList();if (cityList != null && !cityList.isEmpty()) {//获取第一个省份的第一个城市名mCurrentCityName = cityList.get(0).getName();List<DistrictModel> districtList = cityList.get(0).getDistrictList();//获取第一个省份的第一个城市的第一个县名称mCurrentDistrictName = districtList.get(0).getName();mCurrentZipCode = districtList.get(0).getZipcode();}}// */mProvinceDatas = new String[provinceList.size()];for (int i = 0; i < provinceList.size(); i++) {// 遍历所有省的数据mProvinceDatas[i] = provinceList.get(i).getName();List<CityModel> cityList = provinceList.get(i).getCityList();String[] cityNames = new String[cityList.size()];for (int j = 0; j < cityList.size(); j++) {// 遍历省下面的所有市的数据cityNames[j] = cityList.get(j).getName();List<DistrictModel> districtList = cityList.get(j).getDistrictList();String[] distrinctNameArray = new String[districtList.size()];DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];for (int k = 0; k < districtList.size(); k++) {// 遍历市下面所有区/县的数据DistrictModel districtModel = new DistrictModel(districtList.get(k).getName(), districtList.get(k).getZipcode());// 区/县对于的邮编,保存到mZipcodeDatasMapmZipcodeDatasMap.put(districtList.get(k).getName(),districtList.get(k).getZipcode());distrinctArray[k] = districtModel;distrinctNameArray[k] = districtModel.getName();}// 市-区/县的数据,保存到mDistrictDatasMapmDistrictDatasMap.put(cityNames[j], distrinctNameArray);}// 省-市的数据,保存到mCitisDatasMapmCitisDatasMap.put(provinceList.get(i).getName(), cityNames);}} catch (Throwable e) {e.printStackTrace();} finally {}}}
主界面MainActivity
package com.mrwujay.cascade.activity;import com.mrwujay.cascade.R;
import com.mrwujay.cascade.R.id;
import com.mrwujay.cascade.R.layout;import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;public class MainActivity extends BaseActivity implements OnClickListener, OnWheelChangedListener {private WheelView mViewProvince;private WheelView mViewCity;private WheelView mViewDistrict;private Button mBtnConfirm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setUpViews();setUpListener();setUpData();}private void setUpViews() {mViewProvince = (WheelView) findViewById(R.id.id_province);mViewCity = (WheelView) findViewById(R.id.id_city);mViewDistrict = (WheelView) findViewById(R.id.id_district);mBtnConfirm = (Button) findViewById(R.id.btn_confirm);}private void setUpListener() {// 添加change事件mViewProvince.addChangingListener(this);// 添加change事件mViewCity.addChangingListener(this);// 添加change事件mViewDistrict.addChangingListener(this);// 添加onclick事件mBtnConfirm.setOnClickListener(this);}private void setUpData() {initProvinceDatas();mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(MainActivity.this, mProvinceDatas));// 设置可见条目数量mViewProvince.setVisibleItems(7);mViewCity.setVisibleItems(7);mViewDistrict.setVisibleItems(7);updateCities();updateAreas();}/*** 根据当前的省,更新市WheelView的信息*/private void updateCities() {//获取当前的省份的itme索引值int pCurrent = mViewProvince.getCurrentItem();//湖区当前省份名字mCurrentProviceName = mProvinceDatas[pCurrent];//获取该省份下面的市数组集合String[] cities = mCitisDatasMap.get(mCurrentProviceName);if (cities == null) {cities = new String[] { "" };}mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));mViewCity.setCurrentItem(0);updateAreas();}/*** 根据当前的市,更新区WheelView的信息*/private void updateAreas() {int pCurrent = mViewCity.getCurrentItem();mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];String[] areas = mDistrictDatasMap.get(mCurrentCityName);if (areas == null) {areas = new String[] { "" };}mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));mViewDistrict.setCurrentItem(0);}/*** 实现接口方法的回调*/@Overridepublic void onChanged(WheelView wheel, int oldValue, int newValue) {// TODO Auto-generated method stubif (wheel == mViewProvince) {updateCities();} else if (wheel == mViewCity) {updateAreas();} else if (wheel == mViewDistrict) {//获取县的名字mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];//获取县的编码mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_confirm:showSelectedResult();break;default:break;}}private void showSelectedResult() {Toast.makeText(MainActivity.this, "当前选中:"+mCurrentProviceName+","+mCurrentCityName+","+mCurrentDistrictName+","+mCurrentZipCode, Toast.LENGTH_SHORT).show();}
}
还有2个drawable
wheel_bg.xml
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"></layer-list>
wheel_val.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><gradientandroid:angle="90"android:centerColor="#70222222"android:endColor="#70EEEEEE"android:startColor="#70222222" /><strokeandroid:width="20dp"android:color="#FF69B4" /></shape>
这篇关于android-wheel三级联动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!