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

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

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

先看下效果图:

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

1、首先导入github上的wheel项目Android 省市县 三级联动(android-wheel的使用)

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

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

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

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

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

[html] view plaincopyAndroid 省市县 三级联动(android-wheel的使用)Android 省市县 三级联动(android-wheel的使用)
  1. {"citylist":  
  2.     [{"p":"河北",  
  3.       "c":[{"n":"石家庄",  
  4.       "a":[{"s":"长安区"},{"s":"桥东区"},{"s":"鹿泉市"}]  
  5.     }]  
  6. }  

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

[html] view plaincopyAndroid 省市县 三级联动(android-wheel的使用)Android 省市县 三级联动(android-wheel的使用)
  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 plaincopyAndroid 省市县 三级联动(android-wheel的使用)Android 省市县 三级联动(android-wheel的使用)
  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 

相关文章: