在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

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

2017-2-7 13:39| 发布者: zhangjf| 查看: 548| 评论: 0

摘要: 转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23382805 今天没事跟群里面侃大山,有个哥们说道Android Wheel这个控件,以为是Andriod内置的控件,google一把,发现是个github上的一个控件。 ...

转载请注明出处: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中:

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

  1. {"citylist":
  2. [{"p":"河北",
  3. "c":[{"n":"石家庄",
  4. "a":[{"s":"长安区"},{"s":"桥东区"},{"s":"鹿泉市"}]
  5. }]
  6. }
复制代码

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

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

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


源码下载点击这里



来自: http://blog.csdn.net//lmj623565791/article/details/23382805

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 00:46

Copyright 2015-2025 djqfx

返回顶部