日历

2019-05-24  本文已影响0人  沈溺_16e5
依赖
//    侧滑菜单、TabLayout
implementation 'com.android.support:design:28.0.0'
用法

直接调用就可以

DateUtil.getDateTime(CompileActivity.this,tvDate);
DateUtil
public class DateUtil {
    public static void getDateTime(Context mContext, final TextView textView) {
        View date_time_picker = View.inflate(mContext, R.layout.layout_date_picker, null);
        final DatePicker datePicker = (DatePicker) date_time_picker.findViewById(R.id.data_picker);

        //   Build   DateTimeDialog
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(date_time_picker);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int month = datePicker.getMonth();
                int newMonth = month + 1;
                String dateStr = datePicker.getYear() + "-" + newMonth + "-" + datePicker.getDayOfMonth();
//                int currentMinute = timePicker.getCurrentMinute();
//                String curMinute = "";
//                if (currentMinute < 10) {
//                    curMinute = "0" + currentMinute;
//                } else {
//                    curMinute = String.valueOf(currentMinute);
//                }
//                String timeStr = timePicker.getCurrentHour() + ":" + curMinute;
                textView.setText(dateStr);
            }
        });
        builder.show();
    }
}
layout_date_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <DatePicker
        android:id="@+id/data_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </DatePicker>

</LinearLayout>
日历.png
上一篇下一篇

猜你喜欢

热点阅读