自定义时间选择器

非 Y 不嫁゛ 提交于 2019-11-28 16:22:16

/**
 * 时间选择器
 * 
 */

public class DatePicker {
    private Context mContext;
    private SparseArray<View> mViews;
    private int mlayoutResId;
    private PopupWindow popupWindow;
    private View vPopupWindow;

    List<String> textList1 = new ArrayList<>();
    List<String> textList2 = new ArrayList<>();
    List<String> textList3 = new ArrayList<>();
    List<String> textList4 = new ArrayList<>();

    private int year;
    private int month;
    private int day;
    private String time;
    private int hour;
    private int howday;
    private String week;
    private int minute;

    private  WheelView wheelView1;
    private  WheelView wheelView2;
    private  WheelView wheelView3;
    private  WheelView wheelView4;

    private TextView tvCancel;

    public DatePicker(Context context, int pop_show, View view) {
        this.mContext = context;
        this.mlayoutResId = pop_show;
        this.mViews = new SparseArray<>();
    }
    public void ShowPickerPopupWindow(){

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vPopupWindow = inflater.inflate(mlayoutResId, null, false);//引入弹窗布局
        popupWindow = new PopupWindow(vPopupWindow, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        //引入依附的布局
        View parentView = LayoutInflater.from(mContext).inflate(R.layout.activity_main, null);
        popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
        popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
        addBackground();

        //初始化控件
        tvCancel = (TextView)vPopupWindow.findViewById(R.id.tv_cancel);
        wheelView1 = (WheelView) vPopupWindow.findViewById(R.id.wheelview);
        wheelView2 = (WheelView) vPopupWindow.findViewById(R.id.wheelview2);
        wheelView3 = (WheelView) vPopupWindow.findViewById(R.id.wheelview3);
        wheelView4 = (WheelView) vPopupWindow.findViewById(R.id.wheelview4);

        //获取当前时间
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);
        time = hour+":"+minute;

        initYear();
        initMonth();
        initDay();
        initTime();

        //取消
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });


    }

    //添加弹窗半透明背景
    private void addBackground() {
        // 主界面变暗
        WindowManager.LayoutParams params = ((Activity) mContext).getWindow().getAttributes();
        params.alpha = 0.7f;
        ((Activity) mContext).getWindow().setAttributes(params);

        //dismiss时恢复原样
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
                lp.alpha = 1f;
                ((Activity) mContext).getWindow().setAttributes(lp);
            }
        });
    }

    /**
     * 初始化年
     */
    private void initYear() {
        for (int i = 2000;i<=2100;i++){
            textList1.add(i+"");
        }
        wheelView1.setWheelAdapter(new ArrayWheelAdapter(mContext)); // 文本数据源
        wheelView1.setSkin(WheelView.Skin.Holo); // common皮肤
        wheelView1.setLoop(true);
        wheelView1.setWheelData(textList1);  // 数据集合
        wheelView1.setSelection(year-2000);
        wheelView1.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onItemSelected(int position, Object o) {
                year = Integer.parseInt(textList1.get(position));
                setDay();
            }
        });
    }

    /**
     * 初始化月
     */
    private void initMonth() {
        for (int i = 1;i<=12;i++){
            textList2.add( String.format("%02d", i) );
        }

        wheelView2.setWheelAdapter(new ArrayWheelAdapter(mContext)); // 文本数据源
        wheelView2.setSkin(WheelView.Skin.Holo); // common皮肤
        wheelView2.setWheelData(textList2);  // 数据集合
        wheelView2.setLoop(true);
        wheelView2.setSelection(month);
        wheelView2.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onItemSelected(int position, Object o) {
                month = Integer.parseInt(textList2.get(position));
                setDay();
            }
        });
    }

    /**
     * 初始化日
     */
    private void initDay() {
        howday=getDay(year,month);  //获取本月的天数
        for (int i = 1;i<=howday;i++){
            textList3.add( String.format("%02d", i) );
        }
        wheelView3.setWheelAdapter(new ArrayWheelAdapter(mContext)); // 文本数据源
        wheelView3.setSkin(WheelView.Skin.Holo); // common皮肤
        wheelView3.setWheelData(textList3);  // 数据集合
        wheelView3.setLoop(true);
        wheelView3.setSelection(day+2);
    }

    /**
     * 设置日
     */
    private void setDay() {
        textList3.clear();  //清空集合
        howday=getDay(year,month);  //获取本月的天数
        for (int i = 1;i<=howday;i++){
            textList3.add( String.format("%02d", i) );
        }
        wheelView3.setWheelData(textList3);  // 数据集合
        wheelView3.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onItemSelected(int position, Object o) {
                day = Integer.parseInt(textList3.get(position));
            }
        });
    }

    /**
     * 初始化时分
     */
    private void initTime() {
        for (int i = 0 ; i < 24 ; i++){
            for (int j = 0 ; j<=5 ; j++){
                textList4.add(String.format("%02d",i)+":"+j+"0");
            }
        }
        wheelView4.setWheelAdapter(new ArrayWheelAdapter(mContext)); // 文本数据源
        wheelView4.setSkin(WheelView.Skin.Holo); // common皮肤
        wheelView4.setLoop(true);
        wheelView4.setWheelData(textList4);  // 数据集合
        setSelection(wheelView4,hour,minute);
        wheelView4.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onItemSelected(int position, Object o) {
                time = textList4.get(position);
            }
        });
    }

    /**
     * 根据当前时间设置默认选中项
     * @param wheelView4
     * @param hour
     * @param minute
     */
    private void setSelection(WheelView wheelView4, int hour, int minute) {
        if (minute<10){
            wheelView4.setSelection((hour *6)+0);
        } else if (minute>=10&&minute<20){
            wheelView4.setSelection((hour *6)+1);
        }else if (minute>=20&&minute<30){
            wheelView4.setSelection((hour *6)+2);
        } else if (minute>=30&&minute<40){
            wheelView4.setSelection((hour *6)+3);
        }else if (minute>=40&&minute<50){
            wheelView4.setSelection((hour *6)+4);
        }else if (minute>=50&&minute<60){
            wheelView4.setSelection((hour *6)+5);
        }
    }

    /**
     *根据是否闰年和月份判断本月的天数
     * @param year
     * @param month
     * @return
     */
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;

        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    /**
     *  基姆拉尔森计算公式根据日期判断星期几
     * @param y
     * @param m
     * @param d
     * @return
     */
    public String CalculateWeekDay(int y, int m, int d) {
        if (m == 1 || m == 2) {
            m += 12;
            y--;
        }
        int iWeek = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;

        switch (iWeek) {
            case 0:
                week="星期一";
                break;
            case 1:
                week="星期二";
                break;
            case 2:
                week="星期三";
                break;
            case 3:
                week="星期四";
                break;
            case 4:
                week="星期五";
                break;
            case 5:
                week="星期六";
                break;
            case 6:
                week="星期日";
                break;
        }
        return week;
    }
    /**
     * 根据id获取view
     *
     * @param viewId
     * @return
     */
    public View getItemView(int viewId) {
        if (popupWindow != null) {
            return popupWindow.getContentView().findViewById(viewId);
        }
        return null;
    }

    /**
     * 根据id设置pop内部的控件的点击事件的监听
     *
     * @param viewId
     * @param listener
     */
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getItemView(viewId);
        view.setOnClickListener(listener);
    }

    /**
     * popup 消失
     */
    public void dismiss() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    /**
     * 获取月日
     * @return
     */
    public String getDate(){
        return month+"月"+day+"日";
    }

    /**
     * 获取星期
     * @return
     */
    public String getWeek(){
        return CalculateWeekDay(year, month, day);
    }

    /**
     * 获取时分
     * @return
     */
    public String getTime(){
        return time;
    }

}

https://github.com/wangjiegulu/WheelView

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!