android开发之wheel控件使用详解

出门在外生不起病呀,随便两盒药60多块钱。好吧,不废话了,今天我们来看看wheel控件的使用,这是GitHub上的一个开源控件,用起来十分方便,我们可以用它做许多事情,比如做一个自定义的datepicker,在一些电商App中,经常用它来做省市县三级联动,总之用途还是挺多的,我们今天就一起来看看怎么使用这个东东。

我们先来看看今天要做的一个效果图:

这是我们今天要做的效果图。下面就开始吧。

1.获得wheel

wheel是GitHub上的一个开源控件,我们可以直接在GitHub上下载,地址https://github.com/maarek/android-wheel,下载完成之后我们可以把里边的wheel文件直接当作一个library来使用,也可以把wheel里边的Java类和xml文件拷贝到我们的项目中使用。

2.使用方法

首先我们来看看主布局文件:

<RelativeLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="请选择城市" /><LinearLayoutandroid:id="@+id/content"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@id/title"android:background="@drawable/layout_bg"android:orientation="horizontal" ><kankan.wheel.widget.WheelViewandroid:id="@+id/province_view"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1" ></kankan.wheel.widget.WheelView><kankan.wheel.widget.WheelViewandroid:id="@+id/city_view"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1" ></kankan.wheel.widget.WheelView><kankan.wheel.widget.WheelViewandroid:id="@+id/area_view"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1" ></kankan.wheel.widget.WheelView></LinearLayout><Buttonandroid:id="@+id/confirm"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/content"android:onClick="onClick"android:text="确定" /></RelativeLayout>好了,在主布局文件中我们用到了三个WheelView,分别用来表示省市县,在MainActivity中,我们首先要拿到这三个控件:

provinceView = (WheelView) this.findViewById(R.id.province_view);cityView = (WheelView) this.findViewById(R.id.city_view);areaView = (WheelView) this.findViewById(R.id.area_view);拿到之后,我们要使用ArrayWheelAdapter数据适配器来进行数据适配,这里需要两个参数,一个是上下文,另外一个是一个数组,这个数组就是我们要展示的内容,也就是说我们要把省、市、区县都存为数组的形式,但是考虑到一个省对应多个市,一个市对应多个区县,为了把省市县之间关联起来,我们还要用到一个Map集合,因此,我们设计的数据结构是这样的:

/** * 省 */private String[] provinceArray;/** * 省-市 */private Map<String, String[]> citiesMap;/** * 市-区县 */private Map<String, String[]> areasMap;第一个数组中存所有省的数据,第二个Map中存所有省对应的市的数据,第三个Map中存所有市对应的区县的数据,我们现在要给这是三个数据集赋值,先来看看我们的json数据格式:

[{"name":"北京","city":[{"name":"北京","area":["东城区","西城区","崇文区","宣武区"…]}]}…..]我们的json数据就是这样一种格式,json数据存在assets文件夹中,下面我们看看怎么解析json数据并赋值给上面三个数据集:

private void initJson() {citiesMap = new HashMap<String, String[]>();areasMap = new HashMap<String, String[]>();InputStream is = null;try {StringBuffer sb = new StringBuffer();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"));}JSONArray ja = new JSONArray(sb.toString());provinceArray = new String[ja.length()];String[] citiesArr = null;for (int i = 0; i < provinceArray.length; i++) {JSONObject jsonProvince = ja.getJSONObject(i);provinceArray[i] = jsonProvince.getString("name");JSONArray jsonCities = jsonProvince.getJSONArray("city");citiesArr = new String[jsonCities.length()];for (int j = 0; j < citiesArr.length; j++) {JSONObject jsonCity = jsonCities.getJSONObject(j);citiesArr[j] = jsonCity.getString("name");JSONArray jsonAreas = jsonCity.getJSONArray("area");String[] areaArr = new String[jsonAreas.length()];for (int k = 0; k < jsonAreas.length(); k++) {areaArr[k] = jsonAreas.getString(k);}areasMap.put(citiesArr[j], areaArr);}citiesMap.put(provinceArray[i], citiesArr);}} catch (IOException e) {e.printStackTrace();} catch (JSONException e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}}}json解析技术上没有难点,这里的逻辑稍微有点复杂,用到了三个嵌套的for循环,大家慢慢琢磨一下其实也不难。好了,当数据集中都有数据之后,我们就可以给三个wheel设置Adapter了:

不要哭,你要努力地往前看,你要相信阳光总在风雨后,你最终会看到彩虹的。

android开发之wheel控件使用详解

相关文章:

你感兴趣的文章:

标签云: