Android_全国省市区-三级联动 时间选择器
2016-10-17 14:36
344 查看
转载请标明出处:http://blog.csdn.net/sh_12345/article/details/52838582
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<String> ProvinceList = new ArrayList<String>(); ArrayList<ArrayList<String>> CityList = new ArrayList<ArrayList<String>>(); ArrayList<ArrayList<ArrayList<String>>> CountyList = new ArrayList<ArrayList<ArrayList<String>>>(); 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 4000 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<String> tempj = new ArrayList<String>(); // 存储第i个省的所有城市的城区名的二维数组 ArrayList<ArrayList<String>> tempk = new ArrayList<ArrayList<String>>(); // 取出第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<String> tempkk = new ArrayList<String>(); // 取出第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 为你布局文件中的控件,在我的布局文件中为TextView3.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 Simpl ce31 eDateFormat("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 使用
在布局中
<?xml version="1.0" encoding="utf-8"?> <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:orientation="vertical"> <CalendarView android:id="@+id/calendar" android:layout_width="match_parent" android:layout_height="340dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="40dp" /> </LinearLayout>
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); } });
QQ群:493753952
下载
相关文章推荐
- 安卓学习笔记---Android-PickerView实现 3D滚轮效果(时间选择器、省市区三级联动,单项选择效果)
- Axure教程:省市县三级联动选择(全国省市区数据)
- Android时间选择器(三级联动修改年月日)
- android 省市县街道地址选择 三级联动 四级联动
- Android 地址选择器,实现省市区三级联动
- Android 省市县 三级联动选择(android-wheel的使用)
- Android 地址选择器,实现省市区三级联动
- Android 省市县 三级联动(android-wheel的使用)
- 省市区县纯js三级联动(改写版:可获取选择值)
- iBatisNet实现全国省市区县三级联动
- android与HTML5相结合实现的省市县三级联动
- Android 省市县 三级联动(android-wheel的使用)
- 全国省市县三级联动(数据完整版)
- Android 省市县 三级联动(android-wheel的使用)
- android开发——用GridView实现省市县三级联动
- android开发——用GridView实现省市县三级联动
- 省市区县(含全国完整数据库)Ajax无刷新三级联动
- javascript实现全国省市县三级联动
- Android 省市县 三级联动(android-wheel的使用)
- (转)javascript实现全国省市县三级联动