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. }  

这样就完成了代码的编写,如果这篇文章对你有帮助,可以顶一个~嘿嘿
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值