时间选择器

导入依赖

//地址选择
implementation 'com.contrarywind:Android-PickerView:4.1.6'
implementation 'com.contrarywind:wheelview:4.0.9'

创建工具类

public class Picker {
    public static void showDatePicker(Context context, final DatePickerDialog.OnDateSetListener listener) {
        showDatePicker(context, getyMCurrentTime(), listener);
    }

    public static void showDatePicker(Context context, String date, final DatePickerDialog.OnDateSetListener listener) {
        final Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        try {
            Date parse = format.parse(date);
            calendar.setTime(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                calendar.setTime(date);
                int yy = calendar.get(Calendar.YEAR);
                int mm = calendar.get(Calendar.MONTH);
                int dd = calendar.get(Calendar.DAY_OF_MONTH);
                listener.onDateSet(null, yy, mm, dd);
            }
        })

                .setType(new boolean[]{true, true, false, false, false, false})
                .setCancelText("取消")
                .setSubmitText("确定")
                .setDate(calendar)
                .isDialog(false)
                .build()
                .show();
    }

    public static void showYMDPicker(Context context, String title, String date, final DatePickerDialog.OnDateSetListener listener) {
        final Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = format.parse(date);
            calendar.setTime(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                calendar.setTime(date);
                int yy = calendar.get(Calendar.YEAR);
                int mm = calendar.get(Calendar.MONTH);
                int dd = calendar.get(Calendar.DAY_OF_MONTH);
                listener.onDateSet(null, yy, mm, dd);
            }
        })

                .setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消")
                .setSubmitText("确定")
                .setDate(calendar)
                .setTitleText("生日选择")
                .isDialog(false)
                .build()
                .show();
    }

    public interface OnOnePickerChecked<T> {
        void onChecked(T t, int i);
    }

    public static <T> void showOnePicker(Context context, String title, final List<T> data, int checkd, final OnOnePickerChecked<T> lis) {
        if (data == null || data.size() == 0)
            return;
        OptionsPickerView<T> build = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (lis != null)
                    lis.onChecked(data.get(options1), options1);
            }
        })
                .setCancelText("取消")
                .setSubmitText("确定")
                .isDialog(false)
                .setTitleText(title)
                .setSelectOptions(checkd)
                .build();
        build.setPicker(data);
        build.show();


    }


    /**
     * 获取当前时间 yyyyMM
     *
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getyMCurrentTime() {
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM");
        return s.format(new Date());
    }


}

activity调用

 Picker.showDatePicker(MineBillActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { // 年月日
                        mYear = year;
                        mMmonth = month+1;
                        select_date.setText(mYear + "-" + mMmonth);
                    }
                });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值