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="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="请选择城市" />

    <LinearLayout
        android: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.WheelView
            android:id="@+id/province_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </kankan.wheel.widget.WheelView>

        <kankan.wheel.widget.WheelView
            android:id="@+id/city_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </kankan.wheel.widget.WheelView>

        <kankan.wheel.widget.WheelView
            android:id="@+id/area_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </kankan.wheel.widget.WheelView>
    </LinearLayout>

    <Button
        android: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了:

	private void initView() {
		provinceView.setViewAdapter(new ArrayWheelAdapter<String>(
				MainActivity.this, provinceArray));
		// 默认显示北京直辖市里边的市(只有北京市)
		cityView.setViewAdapter(new ArrayWheelAdapter<String>(
				MainActivity.this, citiesMap.get("北京")));
		// 默认显示北京市里边的区县
		areaView.setViewAdapter(new ArrayWheelAdapter<String>(
				MainActivity.this, areasMap.get("北京")));

		// 默认显示第一项
		provinceView.setCurrentItem(0);
		// 默认显示第一项
		cityView.setCurrentItem(0);
		// 默认显示第一项
		areaView.setCurrentItem(0);
		// 页面上显示7项
		provinceView.setVisibleItems(7);
		cityView.setVisibleItems(7);
		areaView.setVisibleItems(7);
		// 添加滑动事件
		provinceView.addChangingListener(this);
		cityView.addChangingListener(this);
	}

设置完Adapter之后我们还设置了一些缺省值,都很简单,大家直接看注释即可,我们这里设置了两个监听事件,我们看看:

@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		if (wheel == provinceView) {
			// 更新省的时候不仅要更新市同时也要更新区县
			updateCity();
			updateArea();
		} else if (wheel == cityView) {
			// 更新市的时候只用更新区县即可
			updateArea();
		}
	}

	private void updateArea() {
		// 获得当前显示的City的下标
		int cityIndex = cityView.getCurrentItem();
		// 获得当前显示的省的下标
		int provinceIndex = provinceView.getCurrentItem();
		// 获得当前显示的省的名字
		String proviceName = provinceArray[provinceIndex];
		// 获得当前显示的城市的名字
		String currentName = citiesMap.get(proviceName)[cityIndex];
		// 根据当前显示的城市的名字获得该城市下所有的区县
		String[] areas = areasMap.get(currentName);
		// 将新获得的数据设置给areaView
		areaView.setViewAdapter(new ArrayWheelAdapter<String>(
				MainActivity.this, areas));
		// 默认显示第一项
		areaView.setCurrentItem(0);
	}

	private void updateCity() {
		// 获得当前显示的省的下标
		int currentIndex = provinceView.getCurrentItem();
		// 获得当前显示的省的名称
		String currentName = provinceArray[currentIndex];
		// 根据当前显示的省的名称获得该省中所有的市
		String[] cities = citiesMap.get(currentName);
		// 将新获得的数据设置给cityView
		cityView.setViewAdapter(new ArrayWheelAdapter<String>(
				MainActivity.this, cities));
		// 默认显示第一项
		cityView.setCurrentItem(0);
	}

几乎每行代码都有注释,我就不啰嗦了,最后我们再来看看点击事件:

	public void onClick(View v) {
		// 获得当前显示的省的下标
		int provinceIndex = provinceView.getCurrentItem();
		// 获得当前显示的省的名称
		String provinceName = provinceArray[provinceIndex];
		// 获得当前显示的城市的下标
		int cityIndex = cityView.getCurrentItem();
		// 获得当前显示的城市的名称
		String cityName = citiesMap.get(provinceName)[cityIndex];
		// 获得当前显示的区县的下标
		int areaIndex = areaView.getCurrentItem();
		Toast.makeText(
				this,
				"您选择的地区是" + provinceArray[provinceIndex] + cityName
						+ areasMap.get(cityName)[areaIndex], Toast.LENGTH_SHORT)
				.show();
	}

好了,到这里我们想要的功能基本上就实现了,但是我们可以看到,系统默认的样式略显丑陋,那我我们可以通过修改源码来获得我们想要的样式,首先上下的黑边看这里:

private int[] SHADOWS_COLORS = new int[] { 0xFF111111, 0x00AAAAAA,
			0x00AAAAAA };

在WheelView.java文件中,这一行代码定义了上下黑边的颜色的变化,三个参数分别是起始颜色,过渡颜色以及结束时的颜色,那么我们可以通过修改这里的源码来去掉上下的黑边,还有中间那个透明的东东黑不拉叽的,我们想改,通过源码找到了这个文件wheel_val.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
	<gradient
		android:startColor="#70222222"
		android:centerColor="#70222222"
		android:endColor="#70EEEEEE"
		android:angle="90" />

	<stroke android:width="1dp" android:color="#70333333" />
</shape>

这里定义了中间那个透明条的样式,我们可以根据自己的需要进行修改。好了,这里的源码不多,也不难,大家可以自己去琢磨琢磨,关于wheel的介绍我们就说这么多。

本文Demo下载https://github.com/lenve/wheelTest

版权声明:本文为博主原创文章,未经博主允许不得转载。若有错误地方,还望批评指正,不胜感激。

时间: 2024-08-27 04:31:59

android开发之wheel控件使用详解的相关文章

android开发之ToggleButton控件

ToggleButton可以认为是一个开关,每单击依次一次在"开"和"关"之间进行切换. ToggleButton的几个常用方法: 1.setTextOff() 当未被选中时,显示的文本. 2.setTextOn() 当被选中时,显示的文本. 3.setCheaked() 设置控件的状态. 4.setBackgroundDrawable() 设置控件的的背景. 控件使用很简单. 演示实例: MainActivity.java public class MainAct

Android开发之RatingBar控件

RatingBar控件为用户提供一个评分操作的模式. 控件的几个常用方法: 1.setMax() 设置RatingBar星级滑块的最大值. 2.setNumStars() 设置RatingBar星级滑块的星形数量.值得注意的是,应该把控件的布局宽度设置为wrap_content,如果设置为fill_parent,显示的星形数量很有可能不是设置的星形数量. 3.setRating() 设置RatingBar星级滑块的显示分数,设置星星的数量. 4.setStepSize() 设置RatingBar

Android开发之DatePicker控件与TimePicker控件

DatePicker控件继承自FrameLayout类,日期选择控件的主要功能是向用户提供包含年.月.日的日期数据并允许用户对其修改. TimePicker控件继承自FrameLayout类,向用户显示一天中的时间,允许用户对其进行修改. DatePicker控件的方法: 1.init() 初始化DatePicker日期选择控件,设置初始显示的年份.月份.日数,设置日期更改监听器.其中有一个monthOfYear参数为初始月份,实际显示的月份是此处设置的值加1. 2.OnDateChangedL

Android开发之Spinner控件使用

之前博客中已经介绍了Spinner控件的数据绑定,地址在:http://blog.csdn.net/zdw_wym/article/details/50000357,本篇将介绍Spinner的事件监听器:OnItemSelectedListener . Spinner组件的主要功能是用于进行下拉列表的显示,当用户选中下拉列表中的某个选项后可以使用Spinner类提供的setOnItemSelectedListener()方法进行监听. 在这之前让我们首先了解一下OnItemSelectedLis

Android开发之基本控件和详解四种布局方式

Android中的控件的使用方式和iOS中控件的使用方式基本相同,都是事件驱动.给控件添加事件也有接口回调和委托代理的方式.今天这篇博客就总结一下Android中常用的基本控件以及布局方式.说到布局方式Android和iOS还是区别挺大的,在iOS中有Frame绝对布局和AutoLayout相对布局.而在Android中的布局方式就比较丰富了,今天博客中会介绍四种常用的布局方式.先总结一下控件,然后再搞一搞基本方式,开发环境还是用的Mac下的Android Studio.开始今天的正题, 虽然A

Android开发之AutoCompleteTextView控件

AutoCompleteTextView是一个可编辑的文本,自动显示与用户输入字符串部分匹配和位置匹配的字符串,从中进行选择. 介绍几个方法: 1.setAdapter() 自动完成文本框设置适配器,显示能匹配字符串. 2.setThreshold() 输入多少个字符后开始匹配. 3.setThreshold() 参数为字符串,用户输入的字符具有匹配项时,显示匹配项,并显示该提示信息. 4.setDropDownBackgroundResource()与setDropDownBackground

【转】Android开发之Bitmap的内存优化详解

本文来源:转载自: http://mobile.51cto.com/abased-410796.htm 在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitmap时,分给虚拟机中的图片的堆栈大小只有8M,如果超出了,就会出现OutOfMemory异常.所以,对于图片的内存优化,是Android应用开发中比较重要的内容. 1.要及时回收Bitmap的内存 Bitmap类有一个方法recycle(),从方法名可以看出意思是回收.这里就有疑问了,Android系

android控件开发之Spinner控件

android控件开发之Spinner控件 概述:android中,Spinner控件主要是用来显示下拉列表,同时,用户可以选择列表中的数据,作为当前的选择 java代码: 此代码中使用了两种方法给Spinner提供数据(method 1和method 2).运行时任选其一即可 方法一: 使用的动态list的形式给Spinner提供数据 方法二: 使用的Strings.xml中定义的固定String array提供数据 根据项目需要,选择相关方法即可 package com.example.sp

ArcEngine开发之Command控件使用篇

转自原文 ArcEngine开发之Command控件使用篇 在ArcEngine类库中有大量的Command控件用来与地图控件进行操作和交互.比如有一系列的地图浏览控件.地图查询控件.图斑选取控件.编辑控件来与MapControl和PageLayoutControl进行交互.这些控件被包含在ESRI.ArcGIS.Controls.dll类库中,位于ESRI.ArcGIS.Controls命名空间下. 这些内置的Command控件可以单独实例化来使用,也可以被安置在一个AxToolbarCont