zoukankan      html  css  js  c++  java
  • 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中:

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

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

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:text="请选择城市"
            android:textColor="#ffffff"
            android:textSize="20sp" />
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/layout_bg"
            android:orientation="horizontal" >
    
            <kankan.wheel.widget.WheelView
                android:id="@+id/id_province"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
            </kankan.wheel.widget.WheelView>
    
            <kankan.wheel.widget.WheelView
                android:id="@+id/id_city"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
            </kankan.wheel.widget.WheelView>
    
            <kankan.wheel.widget.WheelView
                android:id="@+id/id_area"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
            </kankan.wheel.widget.WheelView>
        </LinearLayout>
        
        <Button 
    		android:onClick="showChoose"
    		android:layout_gravity="right"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="确定"
            />
        
    
    </LinearLayout>
    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;
    
    /**
     * 
     * @author zhy
     * 
     */
    public class CitiesActivity extends Activity implements OnWheelChangedListener
    {
    	/**
    	 * 把全国的省市区的信息以json的格式保存,解析完成后赋值为null
    	 */
    	private JSONObject mJsonObj;
    	/**
    	 * 省的WheelView控件
    	 */
    	private WheelView mProvince;
    	/**
    	 * 市的WheelView控件
    	 */
    	private WheelView mCity;
    	/**
    	 * 区的WheelView控件
    	 */
    	private WheelView mArea;
    
    	/**
    	 * 所有省
    	 */
    	private String[] mProvinceDatas;
    	/**
    	 * key - 省 value - 市s
    	 */
    	private Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
    	/**
    	 * key - 市 values - 区s
    	 */
    	private Map<String, String[]> mAreaDatasMap = new HashMap<String, String[]>();
    
    	/**
    	 * 当前省的名称
    	 */
    	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<String>(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();
    
    	}
    
    	/**
    	 * 根据当前的市,更新区WheelView的信息
    	 */
    	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<String>(this, areas));
    		mArea.setCurrentItem(0);
    	}
    
    	/**
    	 * 根据当前的省,更新市WheelView的信息
    	 */
    	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<String>(this, cities));
    		mCity.setCurrentItem(0);
    		updateAreas();
    	}
    
    	/**
    	 * 解析整个Json对象,完成后释放Json对象的内存
    	 */
    	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
    				{
    					/**
    					 * Throws JSONException if the mapping doesn't exist or is
    					 * not a JSONArray.
    					 */
    					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
    					{
    						/**
    						 * Throws JSONException if the mapping doesn't exist or
    						 * is not a JSONArray.
    						 */
    						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;
    	}
    
    	/**
    	 * 从assert文件夹中读取省市区的json文件,然后转化为json对象
    	 */
    	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();
    		}
    	}
    
    	/**
    	 * change事件的处理
    	 */
    	@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();
    	}
    }
    

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


    源码下载点击这里



  • 相关阅读:
    textarea中的空格与换行
    js判断微信内置浏览器
    关于express4不再支持body-parser
    html5 geolocation API
    屏幕密度与分辨率
    nodebeginer
    手机浏览器下IScroll中click事件
    iphone手机上的click和touch
    AngularJS学习笔记一
    不用bootstrap实现居中适应
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3752049.html
Copyright © 2011-2022 走看看