android-wheel控件实现三级联动效果
程序员文章站
2024-03-02 17:58:16
本文实例为大家分享了android wheel省市县三级联动效果,供大家参考,具体内容如下
在github上面有一个叫做 android-wheel 的开源控件, 代码地...
本文实例为大家分享了android wheel省市县三级联动效果,供大家参考,具体内容如下
在github上面有一个叫做 android-wheel 的开源控件, 代码地址:
源码下载地址:http://xiazai.jb51.net/201610/yuanma/androidcascademaster(jb51.net).rar
主界面布局
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" > <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <kankan.wheel.widget.wheelview android:id="@+id/id_province" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > </kankan.wheel.widget.wheelview> <kankan.wheel.widget.wheelview android:id="@+id/id_city" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > </kankan.wheel.widget.wheelview> <kankan.wheel.widget.wheelview android:id="@+id/id_district" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > </kankan.wheel.widget.wheelview> </linearlayout> <button android: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; } @override public 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; } @override public 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; } @override public 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; } @override public 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> */ @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 { } }
接下来看基类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(); // 解析xml saxparser 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()); // 区/县对于的邮编,保存到mzipcodedatasmap mzipcodedatasmap.put(districtlist.get(k).getname(), districtlist.get(k).getzipcode()); distrinctarray[k] = districtmodel; distrinctnamearray[k] = districtmodel.getname(); } // 市-区/县的数据,保存到mdistrictdatasmap mdistrictdatasmap.put(citynames[j], distrinctnamearray); } // 省-市的数据,保存到mcitisdatasmap mcitisdatasmap.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; @override protected 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); } /** * 实现接口方法的回调 */ @override public void onchanged(wheelview wheel, int oldvalue, int newvalue) { // todo auto-generated method stub if (wheel == mviewprovince) { updatecities(); } else if (wheel == mviewcity) { updateareas(); } else if (wheel == mviewdistrict) { //获取县的名字 mcurrentdistrictname = mdistrictdatasmap.get(mcurrentcityname)[newvalue]; //获取县的编码 mcurrentzipcode = mzipcodedatasmap.get(mcurrentdistrictname); } } @override public 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" > <gradient android:angle="90" android:centercolor="#70222222" android:endcolor="#70eeeeee" android:startcolor="#70222222" /> <stroke android:width="20dp" android:color="#ff69b4" /> </shape>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android编程中读写私有文件的方法