Android 省市县 三级联动(android-wheel的使用)

简介:

转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23382805

今天没事跟群里面侃大山,有个哥们说道Android Wheel这个控件,以为是Andriod内置的控件,google一把,发现是个github上的一个控件。

下载地址:https://code.google.com/p/android-wheel/    发现很适合做省市县三级联动就做了一个。

先看下效果图:

1、首先导入github上的wheel项目

2、新建个项目,然后选择记得右键->Properties->Android中将wheel添加为lib:


上面两个步骤是导入所有开源项目的过程了。

3、下面开始代码的编写:首先是省市区的json文件,放置在asserts的city.json中:

大概的格式先了解一下,一会代码会根据这样的格式解析

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {"citylist":  
  2.     [{"p":"河北",  
  3.       "c":[{"n":"石家庄",  
  4.       "a":[{"s":"长安区"},{"s":"桥东区"},{"s":"鹿泉市"}]  
  5.     }]  
  6. }  

4、布局文件,比较简单就3个WheelView分别代表省,市,县,还有一个按钮:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#000000"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center"  
  12.         android:layout_margin="10dp"  
  13.         android:text="请选择城市"  
  14.         android:textColor="#ffffff"  
  15.         android:textSize="20sp" />  
  16.   
  17.     <LinearLayout  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:background="@drawable/layout_bg"  
  21.         android:orientation="horizontal" >  
  22.   
  23.         <kankan.wheel.widget.WheelView  
  24.             android:id="@+id/id_province"  
  25.             android:layout_width="0dp"  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_weight="1" >  
  28.         </kankan.wheel.widget.WheelView>  
  29.   
  30.         <kankan.wheel.widget.WheelView  
  31.             android:id="@+id/id_city"  
  32.             android:layout_width="0dp"  
  33.             android:layout_height="wrap_content"  
  34.             android:layout_weight="1" >  
  35.         </kankan.wheel.widget.WheelView>  
  36.   
  37.         <kankan.wheel.widget.WheelView  
  38.             android:id="@+id/id_area"  
  39.             android:layout_width="0dp"  
  40.             android:layout_height="wrap_content"  
  41.             android:layout_weight="1" >  
  42.         </kankan.wheel.widget.WheelView>  
  43.     </LinearLayout>  
  44.       
  45.     <Button   
  46.         android:onClick="showChoose"  
  47.         android:layout_gravity="right"  
  48.         android:layout_width="wrap_content"  
  49.         android:layout_height="wrap_content"  
  50.         android:text="确定"  
  51.         />  
  52.       
  53.   
  54. </LinearLayout>  
5、Activity的编写:注释相当详细,节不赘述了。
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.wheel_province;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. import kankan.wheel.widget.OnWheelChangedListener;  
  9. import kankan.wheel.widget.WheelView;  
  10. import kankan.wheel.widget.adapters.ArrayWheelAdapter;  
  11.   
  12. import org.json.JSONArray;  
  13. import org.json.JSONException;  
  14. import org.json.JSONObject;  
  15.   
  16. import android.app.Activity;  
  17. import android.os.Bundle;  
  18. import android.view.View;  
  19. import android.widget.Toast;  
  20.   
  21. /** 
  22.  *  
  23.  * @author zhy 
  24.  *  
  25.  */  
  26. public class CitiesActivity extends Activity implements OnWheelChangedListener  
  27. {  
  28.     /** 
  29.      * 把全国的省市区的信息以json的格式保存,解析完成后赋值为null 
  30.      */  
  31.     private JSONObject mJsonObj;  
  32.     /** 
  33.      * 省的WheelView控件 
  34.      */  
  35.     private WheelView mProvince;  
  36.     /** 
  37.      * 市的WheelView控件 
  38.      */  
  39.     private WheelView mCity;  
  40.     /** 
  41.      * 区的WheelView控件 
  42.      */  
  43.     private WheelView mArea;  
  44.   
  45.     /** 
  46.      * 所有省 
  47.      */  
  48.     private String[] mProvinceDatas;  
  49.     /** 
  50.      * key - 省 value - 市s 
  51.      */  
  52.     private Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();  
  53.     /** 
  54.      * key - 市 values - 区s 
  55.      */  
  56.     private Map<String, String[]> mAreaDatasMap = new HashMap<String, String[]>();  
  57.   
  58.     /** 
  59.      * 当前省的名称 
  60.      */  
  61.     private String mCurrentProviceName;  
  62.     /** 
  63.      * 当前市的名称 
  64.      */  
  65.     private String mCurrentCityName;  
  66.     /** 
  67.      * 当前区的名称 
  68.      */  
  69.     private String mCurrentAreaName ="";  
  70.   
  71.     @Override  
  72.     protected void onCreate(Bundle savedInstanceState)  
  73.     {  
  74.         super.onCreate(savedInstanceState);  
  75.         setContentView(R.layout.citys);  
  76.   
  77.         initJsonData();  
  78.   
  79.         mProvince = (WheelView) findViewById(R.id.id_province);  
  80.         mCity = (WheelView) findViewById(R.id.id_city);  
  81.         mArea = (WheelView) findViewById(R.id.id_area);  
  82.   
  83.         initDatas();  
  84.   
  85.         mProvince.setViewAdapter(new ArrayWheelAdapter<String>(this, mProvinceDatas));  
  86.         // 添加change事件  
  87.         mProvince.addChangingListener(this);  
  88.         // 添加change事件  
  89.         mCity.addChangingListener(this);  
  90.         // 添加change事件  
  91.         mArea.addChangingListener(this);  
  92.   
  93.         mProvince.setVisibleItems(5);  
  94.         mCity.setVisibleItems(5);  
  95.         mArea.setVisibleItems(5);  
  96.         updateCities();  
  97.         updateAreas();  
  98.   
  99.     }  
  100.   
  101.     /** 
  102.      * 根据当前的市,更新区WheelView的信息 
  103.      */  
  104.     private void updateAreas()  
  105.     {  
  106.         int pCurrent = mCity.getCurrentItem();  
  107.         mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];  
  108.         String[] areas = mAreaDatasMap.get(mCurrentCityName);  
  109.   
  110.         if (areas == null)  
  111.         {  
  112.             areas = new String[] { "" };  
  113.         }  
  114.         mArea.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));  
  115.         mArea.setCurrentItem(0);  
  116.     }  
  117.   
  118.     /** 
  119.      * 根据当前的省,更新市WheelView的信息 
  120.      */  
  121.     private void updateCities()  
  122.     {  
  123.         int pCurrent = mProvince.getCurrentItem();  
  124.         mCurrentProviceName = mProvinceDatas[pCurrent];  
  125.         String[] cities = mCitisDatasMap.get(mCurrentProviceName);  
  126.         if (cities == null)  
  127.         {  
  128.             cities = new String[] { "" };  
  129.         }  
  130.         mCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));  
  131.         mCity.setCurrentItem(0);  
  132.         updateAreas();  
  133.     }  
  134.   
  135.     /** 
  136.      * 解析整个Json对象,完成后释放Json对象的内存 
  137.      */  
  138.     private void initDatas()  
  139.     {  
  140.         try  
  141.         {  
  142.             JSONArray jsonArray = mJsonObj.getJSONArray("citylist");  
  143.             mProvinceDatas = new String[jsonArray.length()];  
  144.             for (int i = 0; i < jsonArray.length(); i++)  
  145.             {  
  146.                 JSONObject jsonP = jsonArray.getJSONObject(i);// 每个省的json对象  
  147.                 String province = jsonP.getString("p");// 省名字  
  148.   
  149.                 mProvinceDatas[i] = province;  
  150.   
  151.                 JSONArray jsonCs = null;  
  152.                 try  
  153.                 {  
  154.                     /** 
  155.                      * Throws JSONException if the mapping doesn't exist or is 
  156.                      * not a JSONArray. 
  157.                      */  
  158.                     jsonCs = jsonP.getJSONArray("c");  
  159.                 } catch (Exception e1)  
  160.                 {  
  161.                     continue;  
  162.                 }  
  163.                 String[] mCitiesDatas = new String[jsonCs.length()];  
  164.                 for (int j = 0; j < jsonCs.length(); j++)  
  165.                 {  
  166.                     JSONObject jsonCity = jsonCs.getJSONObject(j);  
  167.                     String city = jsonCity.getString("n");// 市名字  
  168.                     mCitiesDatas[j] = city;  
  169.                     JSONArray jsonAreas = null;  
  170.                     try  
  171.                     {  
  172.                         /** 
  173.                          * Throws JSONException if the mapping doesn't exist or 
  174.                          * is not a JSONArray. 
  175.                          */  
  176.                         jsonAreas = jsonCity.getJSONArray("a");  
  177.                     } catch (Exception e)  
  178.                     {  
  179.                         continue;  
  180.                     }  
  181.   
  182.                     String[] mAreasDatas = new String[jsonAreas.length()];// 当前市的所有区  
  183.                     for (int k = 0; k < jsonAreas.length(); k++)  
  184.                     {  
  185.                         String area = jsonAreas.getJSONObject(k).getString("s");// 区域的名称  
  186.                         mAreasDatas[k] = area;  
  187.                     }  
  188.                     mAreaDatasMap.put(city, mAreasDatas);  
  189.                 }  
  190.   
  191.                 mCitisDatasMap.put(province, mCitiesDatas);  
  192.             }  
  193.   
  194.         } catch (JSONException e)  
  195.         {  
  196.             e.printStackTrace();  
  197.         }  
  198.         mJsonObj = null;  
  199.     }  
  200.   
  201.     /** 
  202.      * 从assert文件夹中读取省市区的json文件,然后转化为json对象 
  203.      */  
  204.     private void initJsonData()  
  205.     {  
  206.         try  
  207.         {  
  208.             StringBuffer sb = new StringBuffer();  
  209.             InputStream is = getAssets().open("city.json");  
  210.             int len = -1;  
  211.             byte[] buf = new byte[1024];  
  212.             while ((len = is.read(buf)) != -1)  
  213.             {  
  214.                 sb.append(new String(buf, 0, len, "gbk"));  
  215.             }  
  216.             is.close();  
  217.             mJsonObj = new JSONObject(sb.toString());  
  218.         } catch (IOException e)  
  219.         {  
  220.             e.printStackTrace();  
  221.         } catch (JSONException e)  
  222.         {  
  223.             e.printStackTrace();  
  224.         }  
  225.     }  
  226.   
  227.     /** 
  228.      * change事件的处理 
  229.      */  
  230.     @Override  
  231.     public void onChanged(WheelView wheel, int oldValue, int newValue)  
  232.     {  
  233.         if (wheel == mProvince)  
  234.         {  
  235.             updateCities();  
  236.         } else if (wheel == mCity)  
  237.         {  
  238.             updateAreas();  
  239.         } else if (wheel == mArea)  
  240.         {  
  241.             mCurrentAreaName = mAreaDatasMap.get(mCurrentCityName)[newValue];  
  242.         }  
  243.     }  
  244.   
  245.     public void showChoose(View view)  
  246.     {  
  247.         Toast.makeText(this, mCurrentProviceName + mCurrentCityName + mCurrentAreaName, 1).show();  
  248.     }  
  249. }  

这样就完成了代码的编写,如果这篇文章对你有帮助,可以顶一个~嘿嘿~


源码下载点击这里

相关文章
|
5月前
|
运维 安全 Java
为什么不建议从零开发商城?集体放弃自研,转向开源二开的核心原因
电商技术选型核心在于规避隐性成本:自研商城看似自由,实则长期维护、迭代、安全投入巨大;成熟开源系统可省60%无效开发。本文对比VortMall(高并发多业态)、TigShop(全开源Java/低二开成本)、Jinor(PHP轻量快启)等5大主流方案,聚焦架构弹性、源码透明、生态持续与场景匹配,助企业精准降本增效。
508 0
为什么不建议从零开发商城?集体放弃自研,转向开源二开的核心原因
|
5月前
|
数据采集 存储 人工智能
DCMM 2.0 九大能力域技术架构深度解析:从 L2 到 L4 的评估升级路径
DCMM 2.0(GB/T 36073-2025)于2026年7月1日实施,能力域扩至9个、能力项增至33个、评估指标达486项。本文从技术架构视角深度解读九大能力域,结合五级成熟度、量化指标与企业实践,为数据架构师提供标准落地与架构设计的实战参考框架。
|
6月前
|
消息中间件 人工智能 运维
Agentic AICon【智能体基础设施与 AgentOps 专场】精彩回顾 & PPT 下载
Agentic AICon【智能体基础设施与 AgentOps 专场】精彩回顾 & PPT 下载。
|
12月前
|
SQL 自然语言处理 数据可视化
企业数据分析系统建设费用多少?高性价比免费 BI 产品推荐
在数据驱动时代,BI工具助力企业挖掘业务价值。本文推荐10款主流商业智能平台,涵盖瓴羊Quick BI、Power BI、Tableau等,覆盖不同规模与需求,助您低成本、高效实现数据分析与可视化决策。
|
6月前
|
存储 弹性计算 安全
跨境独立站轻量化上云与高可用架构实践
Taoify是国产零代码跨境建站SaaS,深度集成阿里云:1小时极速上线,CDN全球加速保障海外访问,WAF+SSL+TDE全链路安全合规,弹性扩缩容应对大促峰值。轻量部署、开放API、多语言/汇率/SEO一体化,助力中小商家低成本打造高可用独立站。(239字)
|
8月前
|
人工智能 自然语言处理 文字识别
【新手入门级操作open claw】新手入门 OpenClaw 2.6.2 技能选择推荐
OpenClaw 2.6.2(小龙虾)是一款AI驱动的电脑自动化工具,内置5大类实用Skill技能:文件管理、办公自动化、浏览器操作、系统管理与内容处理。支持自然语言指令,如“整理桌面”“生成周报”“抓取网页数据”,零代码上手。新手推荐启用5项核心技能,一键提升办公效率。
|
安全 Linux
CentOS下载ISO镜像的方法
访问CentOS官方网站(https://www.centos.org/download/),在“Downloads”页面找到ISO镜像下载链接,选择所需版本和架构(如x86_64)开始下载。CentOS分为Linux版和Stream版,前者每两年发行一次并提供10年安全维护,后者为滚动更新。旧版本可在Vault(https://vault.centos.org/)下载。建议选择DVD格式镜像,包含完整系统和常用软件。
41656 16
CentOS下载ISO镜像的方法
|
存储 JSON 测试技术
GAIA: 一个严苛的智能体基准 简要概括
目前有 乱糟糟的一堆 规划策略,所以我们选择了一个相对简单的预先计划工作流程。每隔 N 步,我们生成两件事情: • 我们已知或可以从上下文中推导出的事实摘要和需要发现的事实 • 基于新观察和上述事实摘要,逐步制定解决任务的计划 可以调整参数 N 以在目标用例中获得更好的性能: 我们为管理智能体选择了 N=2,为网页搜索智能体选择了 N=5。 一个有趣的发现是,如果我们不提供计划的先前版本作为输入,得分会提高。直观的解释是,LLM 通常对上下文中任何相关信息有强烈的偏向。如果提示中存在先前版本的计划,LLM 可能会大量重复使用它,而不是在需要时重新评估方法并重新生成计划。 然后,将事实摘要和计划
817 1
|
存储 Kubernetes 监控
在K8S中,calico有哪些组件?
在K8S中,calico有哪些组件?