时间弹窗 年 月 季 周(下)

接上一篇文章继续时间弹窗 年 月 季 周(上)
代码如下:
showDatePop()一开始调用 得到年月日一些基本信息 和点解事件 这里默认显示当月第一周的时间
finalStartTime和finalEndTime 表示最终选择的时间 无论选择 季 月 周 最后都会用 年 月 日 拼接 比如 2019-1-16 的格式。

	private String selectDate = "";
    private PopupWindow popupWindow;
    private WheelView wl_year, wl_month,wl_quarter,wl_week;
    private int currentSelectYear, currentSelectMonth,currentSelectQuarter,currentSelectWeek;
    private int screenWidth;
    private String currentTime;
    private TextView current_time;
    private int currentYear;
    private int currentMonth;
    Map<Integer, WeekRange> maps ;

    private String finalStartTime ;  // 最终时间  拼起来
    private String finalEndTime ;  // 最终时间  拼起来
	public static Activity mActivity ;
    public static CrmHomeDateWindow mCrmHomeDateWindow ;
	
//选择时间弹窗
    public void showDatePop() {
        DisplayMetrics metrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenWidth = metrics.widthPixels;
        currentTime = GetTimeUtil.getYMTime(System.currentTimeMillis());
        currentYear = Integer.valueOf(currentTime.substring(0, 4));
        currentMonth = Integer.valueOf(currentTime.substring(5));
        selectDate = currentTime;

        // 默认周
        isMonth =true;
        isQuarter = false ;
        isWeek =true;

        Date first = GetTimeUtil.firstMonthDate(new Date());
        maps = new HashMap<Integer, WeekRange>();
        getWeekBeginAndEnd(1, first, maps);

        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View popupWindowView = inflater.inflate(R.layout.pop_date_select, null);

        Button tv_ok = (Button) popupWindowView.findViewById(R.id.btn_ok);
        Button tv_cancel_time = (Button) popupWindowView.findViewById(R.id.btn_cancel);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dateWindowClickListener != null){
                    dateWindowClickListener.onDateWindowClick(finalStartTime,finalEndTime);
                    popupWindow.dismiss();
                }
            }
        });


        popupWindow = new PopupWindow(popupWindowView, screenWidth * 4 / 5, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        popupWindow.setContentView(popupWindowView);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        initWheelView(popupWindowView);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                makeWindowLight();
            }
        });

        tv_cancel_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });


        wl_year.addChangingListener(this);
        wl_month.addChangingListener(this);
        wl_quarter.addChangingListener(this);
        wl_week.addChangingListener(this);

        ImageView tv_last_month = (ImageView) popupWindowView.findViewById(R.id.tv_last_month);
        ImageView tv_next_month = (ImageView) popupWindowView.findViewById(R.id.tv_next_month);
        current_time = (TextView) popupWindowView.findViewById(R.id.tv_current_time);

        Set<Integer> set = maps.keySet();
        for(Integer key : set){

            if(1 == key){
                CrmHomeDateWindow.WeekRange range = maps.get(key);
                current_time.setText(GetTimeUtil.format(range.getBegin())+"-"+GetTimeUtil.format(range.getEnd()));
                String yTime = GetTimeUtil.getYTime(System.currentTimeMillis());
                finalStartTime = yTime+"-"+GetTimeUtil.format(range.getBegin());
                finalEndTime = yTime+"-"+GetTimeUtil.format(range.getEnd());
            }
        }

        tv_last_month.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!isQuarter&&!isWeek){

                    if(isMonth){
                        selectDate = GetTimeUtil.getLastMonth(selectDate, -1); // 月
                        finalStartTime = selectDate+"-01";
                        finalEndTime = selectDate+"-31";
                    }else{
                        selectDate = GetTimeUtil.getLastYear(selectDate, -1); // 年
                        finalStartTime = selectDate+"-01-01"+"";
                        finalEndTime = selectDate+"-12-31"+"";
                    }
                    current_time.setText(selectDate);

                }
                else if(isWeek&&!isQuarter){
                    index2 --;
                    if(index2<0){
                        index2 = 5 ;
                    }

                    Set<Integer> set = maps.keySet();
                    for(Integer key : set){
                        if(key == index2){
                            CrmHomeDateWindow.WeekRange range = maps.get(key);
                            current_time.setText(GetTimeUtil.format(range.getBegin())+"-"+GetTimeUtil.format(range.getEnd()));
                            if(currentSelectYear != 0){
                                finalStartTime = currentSelectYear+"-"+GetTimeUtil.format(range.getBegin());
                                finalEndTime = currentSelectYear+"-"+GetTimeUtil.format(range.getEnd());
                            }else {
                                String yTime = GetTimeUtil.getYTime(System.currentTimeMillis());
                                finalStartTime = yTime+"-"+GetTimeUtil.format(range.getBegin());
                                finalEndTime = yTime+"-"+GetTimeUtil.format(range.getEnd());
                            }
                        }
                    }
                }
                else{ // 季
                    index -- ;
                    if(index<0){
                        index = 3 ;
                    }
                    current_time.setText(quarterData[index]);
                    String quarterDatum = quarterData[index];
                    String[] split = quarterDatum.split("~");
                    String Year = String.valueOf(currentSelectYear); // 先获取哪年的季
                    finalStartTime = Year+"-"+split[0];
                    finalEndTime = Year+"-"+split[1];
                }
            }
        });

        tv_next_month.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isQuarter&&!isWeek) {
                    if (isMonth) {
                        selectDate = GetTimeUtil.getLastMonth(selectDate, 1); // 月
                        finalStartTime = selectDate+"-01";
                        // 判断哪年那月有几天
                        String[] split = selectDate.split("-");
                        int i = Integer.parseInt(split[0]);

                        finalEndTime = selectDate+"-31";
                    } else {
                        selectDate = GetTimeUtil.getLastYear(selectDate, 1); // 年
                        finalStartTime = selectDate+"-01-01"+"";
                        finalEndTime = selectDate+"-12-31"+"";
                    }
                    current_time.setText(selectDate);
                }
                else if(isWeek&&!isQuarter){
                    index2 ++;
                    if(index2>5){
                        index2 = 1 ;
                    }
                    Set<Integer> set = maps.keySet();
                    for(Integer key : set){
                        if(index2 == key){
                            CrmHomeDateWindow.WeekRange range = maps.get(key);
                            current_time.setText(GetTimeUtil.format(range.getBegin())+"-"+GetTimeUtil.format(range.getEnd()));
                            if(currentSelectYear != 0){
                                finalStartTime = currentSelectYear+"-"+GetTimeUtil.format(range.getBegin());
                                finalEndTime = currentSelectYear+"-"+GetTimeUtil.format(range.getEnd());
                            }else {
                                String yTime = GetTimeUtil.getYTime(System.currentTimeMillis());
                                finalStartTime = yTime+"-"+GetTimeUtil.format(range.getBegin());
                                finalEndTime = yTime+"-"+GetTimeUtil.format(range.getEnd());
                            }

                        }
                    }
                }
                else{
                    index ++ ;
                    if(index>3){
                        index = 0 ;
                    }
                    current_time.setText(quarterData[index]);
                    String quarterDatum = quarterData[index];
                    String[] split = quarterDatum.split("~");
                    String Year = String.valueOf(currentSelectYear); // 先获取哪年的季
                    finalStartTime = Year+"-"+split[0];
                    finalEndTime = Year+"-"+split[1];

                }
            }
        });
        final TextView tv1 = (TextView) popupWindowView.findViewById(R.id.text1);
        final TextView tv2 = (TextView) popupWindowView.findViewById(R.id.text2);
        final TextView tv3 = (TextView) popupWindowView.findViewById(R.id.text3);
        final TextView tv4 = (TextView) popupWindowView.findViewById(R.id.text4);

        tv1.setOnClickListener(new View.OnClickListener() { // 年
            @Override
            public void onClick(View v) {
                isMonth =false;
                isQuarter = false;
                isWeek =false;
                currentTime = GetTimeUtil.getYTime(System.currentTimeMillis());
                current_time.setText(currentTime); // 2020
                // 拼接月份
                finalStartTime = currentTime+"-01-01"+"";
                finalEndTime = currentTime+"-12-31"+"";
                initWheelView(popupWindowView);

                tv1.setTextColor(Color.parseColor("#FFFFFF"));
                tv1.setBackgroundResource(R.drawable.shape_rectangle_blue);

                setBackgroundResource(tv2,tv3,tv4);
                setTextBackgroundResource(tv2,tv3,tv4);
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() { // 年月
            @Override
            public void onClick(View v) {
                isMonth =true;
                isQuarter = false ;
                isWeek =false;
                currentTime = GetTimeUtil.getYMTime(System.currentTimeMillis());
                current_time.setText(currentTime);
                finalStartTime = currentTime+"-01";
                finalEndTime = currentTime+"-31";
                initWheelView(popupWindowView);
                tv2.setTextColor(Color.parseColor("#FFFFFF"));
                tv2.setBackgroundResource(R.drawable.shape_rectangle_blue);
                setBackgroundResource(tv1,tv3,tv4);
                setTextBackgroundResource(tv1,tv3,tv4);
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() { // 季度
            @Override
            public void onClick(View v) {
                isMonth =false;
                isQuarter = true ;
                isWeek =false;
                current_time.setText("1.1-3.31");
                // 获取当年
                String yTime = GetTimeUtil.getYTime(System.currentTimeMillis());
                // 拼接月份
                finalStartTime = yTime+"-01-01"+"";
                finalEndTime = yTime+"-03-31"+"";

                initWheelView(popupWindowView);
                tv3.setTextColor(Color.parseColor("#FFFFFF"));
                tv3.setBackgroundResource(R.drawable.shape_rectangle_blue);
                setBackgroundResource(tv1,tv2,tv4);
                setTextBackgroundResource(tv1,tv2,tv4);
            }
        });
        tv4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                isMonth =true;
                isQuarter = false ;
                isWeek =true;
                Date first = GetTimeUtil.firstMonthDate(new Date());
                maps = new HashMap<Integer, CrmHomeDateWindow.WeekRange>();

                getWeekBeginAndEnd(1, first, maps);
                Set<Integer> set = maps.keySet();
                for(Integer key : set){
                    if(key == 1){
                        CrmHomeDateWindow.WeekRange range = maps.get(key);
                        current_time.setText(GetTimeUtil.format(range.getBegin())+"-"+GetTimeUtil.format(range.getEnd()));
                        String yTime = GetTimeUtil.getYTime(System.currentTimeMillis());
                        finalStartTime = yTime+"-"+GetTimeUtil.format(range.getBegin());
                        finalEndTime = yTime+"-"+GetTimeUtil.format(range.getEnd());

                    }
                    CrmHomeDateWindow.WeekRange range = maps.get(key);
//                    Log.e("TAG",(String.format("第%d周,开始日期:%s,结束日期:%s", key,GetTimeUtil.format(range.getBegin()),GetTimeUtil.format(range.getEnd()))));
                }
                initWheelView(popupWindowView);
                tv4.setTextColor(Color.parseColor("#FFFFFF"));
                tv4.setBackgroundResource(R.drawable.shape_rectangle_blue);
                setBackgroundResource(tv1,tv2,tv3);
                setTextBackgroundResource(tv1,tv2,tv3);
            }
        });

        popupWindow.showAtLocation(mActivity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        makeWindowDark();
    }

** 每周开始结束时间方法:**

public  void getWeekBeginAndEnd(int index,Date currentDate,Map<Integer,CrmHomeDateWindow.WeekRange> maps){
        //月末
        Date lastMonthDate = GetTimeUtil.lastMonthDate(currentDate);
        int week = GetTimeUtil.week(currentDate);
        if(null == maps){
            CrmHomeDateWindow.WeekRange range = new CrmHomeDateWindow.WeekRange(currentDate, currentDate);
            maps = new HashMap<Integer, CrmHomeDateWindow.WeekRange>();
            maps.put(index,range);
        }else{
            CrmHomeDateWindow.WeekRange range = maps.get(index);
            if(null == range){
                range = new CrmHomeDateWindow.WeekRange(currentDate);
            }
            range.setEnd(currentDate);
            maps.put(index,range);
        }

        if(currentDate.equals(lastMonthDate)){
            return;
        }

        if(week == 0){
            index++;
        }

        getWeekBeginAndEnd(index,GetTimeUtil.nextDate(currentDate),maps);
    }

onChanged 回调方法 是滑动控件触发的回调方法

@Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        switch (wheel.getId()){

            case R.id.wl_year:

                setDate();
                break;
            case R.id.wl_month:
                setDate();
                break;
            case R.id.wl_quarter:
                setDate();
                break;
            case R.id.wl_week:
                setDate();
                break;
        }
    }
	private int index = 0 ;
    private int index2 = 0 ;
    private String [] quarterData = {"1-1~3-31","4-1~6-30","7-1~9-30","10-1~12-31"};
    private boolean isMonth = true ; // 月
    private boolean isQuarter = false ;// 季
    private boolean isWeek = true ;// 周
    private void setDate(){

        currentSelectYear = wl_year.getCurrentItem() + 1970;//年
        currentSelectMonth = wl_month.getCurrentItem() + 1;//月
        currentSelectQuarter = wl_quarter.getCurrentItem() + 1;//季
        currentSelectWeek = wl_week.getCurrentItem()+1;//周
        if(isMonth && !isWeek){
            selectDate = String.valueOf(currentSelectYear) + "-" + String.format("%02d", currentSelectMonth);
            current_time.setText(selectDate);
            int day = GetTimeUtil.getDay(currentSelectYear, currentSelectMonth);//这个月总共有几天
            finalStartTime = selectDate+"-01";
            finalEndTime = selectDate+"-"+day;
            return;
        }
        if(isQuarter) {
            current_time.setText(quarterData[currentSelectQuarter-1]);
            String Year = String.valueOf(currentSelectYear); // 先获取哪年的季
            String quarterDatum = quarterData[currentSelectQuarter - 1];// 获取当前季度 拆分 拼接
            String[] split = quarterDatum.split("~");
            finalStartTime = Year+"-"+split[0];
            finalEndTime = Year+"-"+split[1];
            return;
        }
        if(isWeek&&isMonth){

            String s = currentSelectYear+"-"+currentSelectMonth;
            Date date = null;
            try {
                date = GetTimeUtil.getDateFormat().parse(s);
            } catch (ParseException e) {
            }
            Date first = GetTimeUtil.firstMonthDate(date);
            maps = new HashMap<Integer, CrmHomeDateWindow.WeekRange>();
            getWeekBeginAndEnd(1, first, maps);
            Set<Integer> set = maps.keySet();
            for(Integer key : set){
                CrmHomeDateWindow.WeekRange range = maps.get(key);
                if(currentSelectWeek == key){
                    current_time.setText(GetTimeUtil.format(range.getBegin())+"~"+GetTimeUtil.format(range.getEnd()));

                    finalStartTime = currentSelectYear+"-"+GetTimeUtil.format(range.getBegin());
                    finalEndTime = currentSelectYear+"-"+GetTimeUtil.format(range.getEnd());
//                    Log.e("TAG",(String.format("第%d周,开始日期:%s,结束日期:%s", key,GetTimeUtil.format(range.getBegin()),GetTimeUtil.format(range.getEnd()))));
                    break;
                }
            }
            return;
        }

        selectDate = String.valueOf(currentSelectYear);
        current_time.setText(selectDate);
        finalStartTime = selectDate+"-01-01"+"";
        finalEndTime = selectDate+"-12-31"+"";
    }

initWheelView方法 初始化时间控件的方法 在点击切换 年月周季 里也用到 主要作用是切换对应的样式和显示默认时间

private void initWheelView(View view) {
        Calendar c = Calendar.getInstance();
        int curYear = c.get(Calendar.YEAR);
        int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        wl_year = (WheelView) view.findViewById(R.id.wl_year);
        wl_month = (WheelView) view.findViewById(R.id.wl_month);
        wl_quarter = (WheelView) view.findViewById(R.id.wl_quarter);
        wl_week = (WheelView) view.findViewById(R.id.wl_week);

        NumericWheelAdapter numericWheelAdapterStart1 = new NumericWheelAdapter(mActivity, 1970, 2100);
        numericWheelAdapterStart1.setLabel("年");
        wl_year.setViewAdapter(numericWheelAdapterStart1);
        numericWheelAdapterStart1.setTextColor(R.color.colorBlack_000000);
        numericWheelAdapterStart1.setTextSize(20);
        wl_year.setCyclic(true);//是否可循环滑动


        if(isMonth){
            wl_month.setVisibility(View.VISIBLE);
            NumericWheelAdapter numericWheelAdapterStart2 = new NumericWheelAdapter(mActivity, 1, 12, "%02d");
            numericWheelAdapterStart2.setLabel("月");
            wl_month.setViewAdapter(numericWheelAdapterStart2);
            numericWheelAdapterStart2.setTextColor(R.color.colorBlack_000000);
            numericWheelAdapterStart2.setTextSize(20);
            wl_month.setCyclic(true);
        }else {
            wl_month.setVisibility(View.GONE);
        }

        if(isQuarter){
            wl_quarter.setVisibility(View.VISIBLE);
            NumericWheelAdapter numericWheelAdapterStart3 = new NumericWheelAdapter(mActivity, 1, 4, "%02d");
            numericWheelAdapterStart3.setLabel("季度");
            wl_quarter.setViewAdapter(numericWheelAdapterStart3);
            numericWheelAdapterStart3.setTextColor(R.color.colorBlack_000000);
            numericWheelAdapterStart3.setTextSize(20);
            wl_quarter.setCyclic(true);
        }else {
            wl_quarter.setVisibility(View.GONE);
        }

        if(isWeek){
            wl_week.setVisibility(View.VISIBLE);
            NumericWheelAdapter numericWheelAdapterStart4 = new NumericWheelAdapter(mActivity, 1, 5, "%02d");
            numericWheelAdapterStart4.setLabel("周");
            wl_week.setViewAdapter(numericWheelAdapterStart4);
            numericWheelAdapterStart4.setTextColor(R.color.colorBlack_000000);
            numericWheelAdapterStart4.setTextSize(20);
            wl_week.setCyclic(true);
        }else {
            wl_week.setVisibility(View.GONE);
        }

        wl_year.setCurrentItem(curYear - 1970);
        wl_month.setCurrentItem(curMonth - 1);
    }
```**自定义类 功能方法**

```java
static class WeekRange{
        Date begin;//周开始日
        Date end;//周结束日
        public WeekRange(Date begin) {
            super();
            this.begin = begin;
        }
        public WeekRange(Date begin, Date end) {
            super();
            this.begin = begin;
            this.end = end;
        }
        public Date getBegin() {
            return begin;
        }
        public void setBegin(Date begin) {
            this.begin = begin;
        }
        public Date getEnd() {
            return end;
        }
        public void setEnd(Date end) {
            this.end = end;
        }
    }

    private void setBackgroundResource(TextView v1,TextView v2,TextView v3 ){
        v1.setBackgroundColor(Color.parseColor("#FFFFFF"));
        v2.setBackgroundColor(Color.parseColor("#FFFFFF"));
        v3.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }
    private void setTextBackgroundResource(TextView v1,TextView v2,TextView v3 ){
        v1.setTextColor(Color.parseColor("#333333"));
        v2.setTextColor(Color.parseColor("#333333"));
        v3.setTextColor(Color.parseColor("#333333"));
    }
    /**
     * 让屏幕变暗
     */
    private void makeWindowDark() {
        Window window = mActivity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 0.5f;
        window.setAttributes(lp);
    }

    /**
     * 让屏幕变亮
     */
    private void makeWindowLight() {
        Window window = mActivity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 1f;
        window.setAttributes(lp);
    }

最后就是一个时间选择后的一个回调接口 返回给条用类 这样就可以实现通用

	public static DateWindowClickListener dateWindowClickListener ;
    public static void setCheckBottonClickListener(DateWindowClickListener dateWindow){
        dateWindowClickListener = dateWindow;
    }
    public interface DateWindowClickListener{
        void onDateWindowClick(String finalStartTime, String finalEndTime);
    }

代码里还用到一个时间管理工具类 名字叫GetTimeUtil(关于已过去时间的帮助类) 网上应该有很多类似的时间工具类 这里代码我就不贴了。

发布了6 篇原创文章 · 获赞 0 · 访问量 191
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览