栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android使用android-wheel实现省市县三级联动

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android使用android-wheel实现省市县三级联动

今天没事跟群里面侃大山,有个哥们说道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中:

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

{"citylist":
 [{"p":"河北",
  "c":[{"n":"石家庄",
  "a":[{"s":"长安区"},{"s":"桥东区"},{"s":"鹿泉市"}]
 }]
}

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



  

  

    
    

    
    

    
    
  
  
  

5、Activity的编写:注释相当详细,节不赘述了。

package com.example.wheel_province;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


public class CitiesActivity extends Activity implements onWheelChangedListener
{
 
 private JSonObject mJsonObj;
 
 private WheelView mProvince;
 
 private WheelView mCity;
 
 private WheelView mArea;

 
 private String[] mProvinceDatas;
 
 private Map mCitisDatasMap = new HashMap();
 
 private Map mAreaDatasMap = new HashMap();

 
 private String mCurrentProviceName;
 
 private String mCurrentCityName;
 
 private String mCurrentAreaName ="";

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.citys);

 initJsonData();

 mProvince = (WheelView) findViewById(R.id.id_province);
 mCity = (WheelView) findViewById(R.id.id_city);
 mArea = (WheelView) findViewById(R.id.id_area);

 initDatas();

 mProvince.setViewAdapter(new ArrayWheelAdapter(this, mProvinceDatas));
 // 添加change事件
 mProvince.addChangingListener(this);
 // 添加change事件
 mCity.addChangingListener(this);
 // 添加change事件
 mArea.addChangingListener(this);

 mProvince.setVisibleItems(5);
 mCity.setVisibleItems(5);
 mArea.setVisibleItems(5);
 updateCities();
 updateAreas();

 }

 
 private void updateAreas()
 {
 int pCurrent = mCity.getCurrentItem();
 mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
 String[] areas = mAreaDatasMap.get(mCurrentCityName);

 if (areas == null)
 {
  areas = new String[] { "" };
 }
 mArea.setViewAdapter(new ArrayWheelAdapter(this, areas));
 mArea.setCurrentItem(0);
 }

 
 private void updateCities()
 {
 int pCurrent = mProvince.getCurrentItem();
 mCurrentProviceName = mProvinceDatas[pCurrent];
 String[] cities = mCitisDatasMap.get(mCurrentProviceName);
 if (cities == null)
 {
  cities = new String[] { "" };
 }
 mCity.setViewAdapter(new ArrayWheelAdapter(this, cities));
 mCity.setCurrentItem(0);
 updateAreas();
 }

 
 private void initDatas()
 {
 try
 {
  JSonArray jsonArray = mJsonObj.getJSonArray("citylist");
  mProvinceDatas = new String[jsonArray.length()];
  for (int i = 0; i < jsonArray.length(); i++)
  {
  JSonObject jsonP = jsonArray.getJSonObject(i);// 每个省的json对象
  String province = jsonP.getString("p");// 省名字

  mProvinceDatas[i] = province;

  JSonArray jsonCs = null;
  try
  {
   
   jsonCs = jsonP.getJSonArray("c");
  } catch (Exception e1)
  {
   continue;
  }
  String[] mCitiesDatas = new String[jsonCs.length()];
  for (int j = 0; j < jsonCs.length(); j++)
  {
   JSonObject jsonCity = jsonCs.getJSonObject(j);
   String city = jsonCity.getString("n");// 市名字
   mCitiesDatas[j] = city;
   JSonArray jsonAreas = null;
   try
   {
   
   jsonAreas = jsonCity.getJSonArray("a");
   } catch (Exception e)
   {
   continue;
   }

   String[] mAreasDatas = new String[jsonAreas.length()];// 当前市的所有区
   for (int k = 0; k < jsonAreas.length(); k++)
   {
   String area = jsonAreas.getJSonObject(k).getString("s");// 区域的名称
   mAreasDatas[k] = area;
   }
   mAreaDatasMap.put(city, mAreasDatas);
  }

  mCitisDatasMap.put(province, mCitiesDatas);
  }

 } catch (JSonException e)
 {
  e.printStackTrace();
 }
 mJsonObj = null;
 }

 
 private void initJsonData()
 {
 try
 {
  StringBuffer sb = new StringBuffer();
  InputStream is = getAssets().open("city.json");
  int len = -1;
  byte[] buf = new byte[1024];
  while ((len = is.read(buf)) != -1)
  {
  sb.append(new String(buf, 0, len, "gbk"));
  }
  is.close();
  mJsonObj = new JSonObject(sb.toString());
 } catch (IOException e)
 {
  e.printStackTrace();
 } catch (JSonException e)
 {
  e.printStackTrace();
 }
 }

 
 @Override
 public void onChanged(WheelView wheel, int oldValue, int newValue)
 {
 if (wheel == mProvince)
 {
  updateCities();
 } else if (wheel == mCity)
 {
  updateAreas();
 } else if (wheel == mArea)
 {
  mCurrentAreaName = mAreaDatasMap.get(mCurrentCityName)[newValue];
 }
 }

 public void showChoose(View view)
 {
 Toast.makeText(this, mCurrentProviceName + mCurrentCityName + mCurrentAreaName, 1).show();
 }
}

源码下载:http://xiazai.jb51.net/201608/yuanma/Androidwheel(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/161580.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号