1 在项目中添加依赖:compile 'com.bigkoo:pickerview:2.1.0'
2从网上下载 WheelView https://github.com/
这里我所用到的有:
ArrayWheelAdapter
OnWheelChangedListener
OnWheelScrollListener
OptionsPopupWindow
ScreenInfo
TimePopupWindow
WheelAdapter
WheelOptions
WheelTime
WheelView
NumericWheelAdapter
3.1 自己写json数据这里是我写的
// 城区数组
ArrayList ProvinceList = new ArrayList();
ArrayList> CityList = new ArrayList>();
ArrayList>> CountyList = new ArrayList>>();
private void getWheelList() {
try {
// 获取json文件输入流
InputStream is = getResources().getAssets().open("china_address.json");
// 将json文件读入为一个字符串
byte[] bytearray = new byte[is.available()];
is.read(bytearray);
String address_json = new String(bytearray, "UTF-8");
// 将json转化为JSONArray对象,这是所有省的JSONArray
JSONArray jsonArraySheng = new JSONArray(address_json);
// 遍历这个JSONArray对象
for (int i = 0; i < jsonArraySheng.length(); i++) {
// 取出第i个省对象,并将其转化为JSONObject对象
JSONObject jsonObjectSheng = jsonArraySheng.getJSONObject(i);
// 将省的名字存入一维数组
StringBuffer provincename = new StringBuffer(jsonObjectSheng.getString("areaName"));
ProvinceList.add(provincename.toString());
// 存储第i个省的城市名的数组
ArrayList tempj = new ArrayList();
// 存储第i个省的所有城市的城区名的二维数组
ArrayList> tempk = new ArrayList>();
// 取出第i个省对象中的城市数组,并将其转化为JSONArray对象
JSONArray jsonArrayShi = jsonObjectSheng.getJSONArray("cities");
// 遍历第i个省的城市JSONArray
for (int j = 0; j < jsonArrayShi.length(); j++) {
// 取出第i个省的第j个市,并将其转化为JSONObject对象
JSONObject jsonObjectShi = jsonArrayShi.getJSONObject(j);
// 将市的名字存入第i个省的城市名数组
StringBuffer cityname = new StringBuffer(jsonObjectShi.getString("areaName"));
tempj.add(cityname.toString());
// 存储第i个省第j个市的城区名的数组
ArrayList tempkk = new ArrayList();
// 取出第i个省第j个市中的城区数组,并将其转化为JSONArray对象
JSONArray jsonArrayQu = jsonObjectShi.getJSONArray("counties");
// 遍历第i个省第j个市的城区JSONArray
for (int k = 0; k < jsonArrayQu.length(); k++) {
// 第i个省第j个市第k个区
JSONObject jsonObjectQu = jsonArrayQu.getJSONObject(k);
// 名字存入数组
StringBuffer countyname = new StringBuffer(jsonObjectQu.getString("areaName"));
tempkk.add(countyname.toString());
}
// 第i个省第j个市的城区名的数组添加到第i个省的所有城市的城区名的二维数组
tempk.add(tempkk);
}
CityList.add(tempj);
CountyList.add(tempk);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
3.2 对 OptionsPopupWindow 进行初始化
getWheelList(); OptionsPopupWindow popupWindow = new OptionsPopupWindow(ProvincialCityActivity.this); popupWindow.showAtLocation(textView, Gravity.BOTTOM, 0, 0);//textView popupWindow.setPicker(ProvinceList, CityList, CountyList, true); popupWindow.setOnoptionsSelectListener(this); popupWindow.setCyclic(false);
其中 textView 为你布局文件中的控件,在我的布局文件中为TextView
3.3 重写OptionsPopupWindow中的方法
@Override
public void onOptionsSelect(int options1, int option2, int options3) {
textView.setText(ProvinceList.get(options1) + CityList.get(options1).get(option2) + CountyList.get(options1).get(option2).get(options3));
Toast.makeText(ProvincialCityActivity.this, "" + textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
这样就实现了全国省市区的三级联动
时间的
TYPE四种选择模式,年月日时分,年月日,时分,月日时分(可改变模式)
4.1 OptionsPopupWindow 进行初始化
type = TimePopupWindow.Type.ALL;
TimePopupWindow popupWindow = new TimePopupWindow(TimeActivity.this, type);
popupWindow.showAtLocation(editText, Gravity.BOTTOM, 0, 0);
popupWindow.setOnTimeSelectListener(this);
popupWindow.setCyclic(true);//是否循环
4.2重写 TimePopupWindow 中的onTimeSelect()方法
因为时间的格式为"Wed Oct 19 17:21:00 GMT+08:00 2016"时区格式,所有需要转换
http://www.chengxuyuans.com/Android/83452.html
@Override
public void onTimeSelect(Date date) {
//String str = "Wed Oct 19 17:21:00 GMT+08:00 2016";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM ddHH:mm:ss 'GMT' yyyy", Locale.US);
try {
String date1 = sdf.format(date);
Date da = sdf.parse(date1);
if (type == TimePopupWindow.Type.ALL) {//年月日时分
sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
Log.i("TAG", "date1 = " + date);
Log.i("TAG", "sdf.format(date1)-- " + sdf.format(da));
editText.setText(sdf.format(da));
} else if (type == TimePopupWindow.Type.YEAR_MONTH_DAY) {//年月日
sdf = new SimpleDateFormat("yyyy年MM月dd日");
Log.i("TAG", "date1 = " + date);
Log.i("TAG", "sdf.format(date1)-- " + sdf.format(da));
editText.setText(sdf.format(da));
} else if (type == TimePopupWindow.Type.MONTH_DAY_HOUR_MIN) {//月日时分
sdf = new SimpleDateFormat("MM月dd日 HH:mm");
Log.i("TAG", "date1 = " + date);
Log.i("TAG", "sdf.format(date1)-- " + sdf.format(da));
editText.setText(sdf.format(da));
} else if (type == TimePopupWindow.Type.HOURS_MINS) {//时分
sdf = new SimpleDateFormat("HH:mm");
Log.i("TAG", "date1 = " + date);
Log.i("TAG", "sdf.format(date1)-- " + sdf.format(da));
editText.setText(sdf.format(da));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
此外还有一些动画、布局、style等
5 CalendarView 使用
在布局中
private CalendarView calendar;
//年/月/日
private int year;
private int month;
private int day;
calendar = (CalendarView) findViewById(R.id.calendar);
Long nowTime = calendar.getDate();
SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日hh:mm:ss");
String time = f.format(nowTime);
System.out.println("-------------" + time);
calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView arg0, int arg1,
int arg2, int arg3) {
arg2 = arg2 + 1;
year = arg1;
month = arg2;
day = arg3;
System.out.println("-------------" + arg1 + "-" + arg2 + "-"
+ arg3);
}
});
下载