依赖
// 侧滑菜单、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
网友评论