这是一个基于wheel的省市县选择器
以下是运行后的效果图
显示的效果可以后面自己设置修改,以下来看下代码
1.以下是城市选择器的布局文件
<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="wrap_content"
android:layout_gravity="bottom"
android:background="#01000000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_cancel"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="center"
android:text="取消" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="请选择城市"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_sure"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="center"
android:text="确定" />
</LinearLayout>
<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="150dp"
android:layout_weight="1" >
</kankan.wheel.widget.WheelView>
<kankan.wheel.widget.WheelView
android:id="@+id/id_city"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1" >
</kankan.wheel.widget.WheelView>
<kankan.wheel.widget.WheelView
android:id="@+id/id_area"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1" >
</kankan.wheel.widget.WheelView>
</LinearLayout>
</LinearLayout>
点击选择城市按钮后,不是通过dialog来显示,而是通过启动一个新的activity来进行实现的
2.在选择城市的activity中,以下的代码是从assets中读取省市县的代码,并且保存到本地
/**
* 从assert文件夹中读取省市区的json文件,然后转化为json对象
*/
private void initJsonData()
{
try
{
StringBuffer sb = new StringBuffer();
InputStream is = getAssets().open("mycity.json");
int len = -1;
byte[] buf = new byte[is.available()];
while ((len = is.read(buf)) != -1)
{
sb.append(new String(buf, 0, len, "utf-8"));
}
is.close();
mJsonObj = new JSONObject(sb.toString());
} catch (IOException e)
{
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//加载字符串中的数据(可以根据资源文件的不同,进行不同的方式进行加载数据)
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;
}
3.通过滑动事件监听并实现联动的效果
/**
* 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];
}
}
/**
* 根据当前的市,更新区WheelView的信息
*/
private void updateAreas()
{
int pCurrent = mCity.getCurrentItem();
mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
String[] areas = mAreaDatasMap.get(mCurrentCityName);
if (areas == null)
{
areas = new String[] { "" };
}
ArrayWheelAdapter<String> mAreaAdapter = new ArrayWheelAdapter<String>(this, areas);
mAreaAdapter.setTextSize(18);
// mAreaAdapter.setTextColor(getResources().getColor(R.color.wheel_blue));//字体的颜色设置
mArea.setViewAdapter(mAreaAdapter);
mArea.setCurrentItem(0);
if(mAreaDatasMap.get(mCurrentCityName)!=null){
mCurrentAreaName = mAreaDatasMap.get(mCurrentCityName)[0];
}
}
/**
* 根据当前的省,更新市WheelView的信息
*/
private void updateCities()
{
int pCurrent = mProvince.getCurrentItem();
mCurrentProviceName = mProvinceDatas[pCurrent];
String[] cities = mCitisDatasMap.get(mCurrentProviceName);
if (cities == null)
{
cities = new String[] { "" };
}
ArrayWheelAdapter<String> mCityAdapter = new ArrayWheelAdapter<String>(this, cities);
mCityAdapter.setTextSize(18);
// mCityAdapter.setTextColor(getResources().getColor(R.color.wheel_blue)); //字体的颜色设置
mCity.setViewAdapter(mCityAdapter);
mCity.setCurrentItem(0);
updateAreas();
}
以上就是主要的实现的代码
以下为源码的下载地址
http://download.csdn.net/detail/shuang__zi/9354029