Android 控件 DatePicker灰常好用的日历控件

拿别人的轮子,表在前边。
DatePicker开源项目地址:https://github.com/AigeStudio/DatePicker

感觉自己手机上的日历真丑,github上搜了一遍,果然发现了一个强大,好用的!(既然自己还是个渣,造不了轮子,那还是先把别人的轮子跑6起来)。

中英文支持,阳历阴历节气显示,交互友好,优点很多。

使用很简单,直接从maven center compile,加上就够了。

compile 'cn.aigestudio.datepicker:DatePicker:2.2.0'

特别注意的一点是,必须给日期一个初始化值,否则报错。

自己写好的一个dialog 样式的

    final AlertDialog dialog = new AlertDialog.Builder(this).create();
    dialog.show();
    DatePicker datapicker = new DatePicker(this);
    datapicker.setMode(DPMode.SINGLE);
    datapicker.setTodayDisplay(true);
    Calendar mcalender = Calendar.getInstance();
    datapicker.setDate(mcalender.get(Calendar.YEAR), mcalender.get(Calendar.MONTH) + 1);
    datapicker.setOnDatePickedListener(new DatePicker.OnDatePickedListener() {
        @Override
        public void onDatePicked(String date) {
            ToastUtil.showShort(date.toString());
            targetdate = date.toString();
            dialog.dismiss();
            tv_time.setText(targetdate);
        }
    });

    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setText("左右滑动切换月份,上下滑动切换年份");
    textView.setTextSize(16.0f);
    textView.setTextColor(DPTManager.getInstance().colorTitle());
    textView.setBackgroundColor(DPTManager.getInstance().colorTitleBG());


    LinearLayout linearLayout = new LinearLayout(context);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.
            LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(0, 0, 0, 0);
    param.setMargins(0, 0, 0, 0);
    linearLayout.addView(datapicker, param);
    linearLayout.addView(textView, param);


    WindowManager windowManager = getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
    lp.width = (int) (display.getWidth()); //设置宽度
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    dialog.getWindow().setAttributes(lp);


    dialog.getWindow().setGravity(Gravity.BOTTOM);
    dialog.getWindow().setContentView(linearLayout, layoutParams);

    dialog.getWindow().setBackgroundDrawable(null);

效果如下:

Android 控件 DatePicker灰常好用的日历控件_第1张图片
Paste_Image.png

你可能感兴趣的:(Android 控件 DatePicker灰常好用的日历控件)