Android_全国省市区-三级联动 时间选择器

 

 

 

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 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 为你布局文件中的控件,在我的布局文件中为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 使用

在布局中

 

<?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);
    }
});
下载

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值