首页 > 解决方案 > 如何在我的应用程序中设置我的 DatePicker 对话框

问题描述

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dd-MM-yyyy, hh:mm:ss, context. GetResources().getConfiguration().locale); 

simpleDateFormat.setTimeZone(TimeZone.getDefault());
return simpleDateFormat.format(new Date(myDate));

此代码仅在我保存笔记时显示日期,它不允许我为我的活动选择日期

标签: android

解决方案


您可以使用以下方式显示 Datepicker 对话框

// Global declaration
Calendar preferDate1 = new GregorianCalendar();
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
private int year, month, day;

在 OnClick 方法中设置下面的代码

DatePickerDialog dp = new DatePickerDialog(MainActivity.this, preferredDate1Picker, preferDate1.get(Calendar.YEAR), preferDate1.get(Calendar.MONTH), preferDate1.get(Calendar.DAY_OF_MONTH));
dp.getDatePicker().setMinDate(c.getTimeInMillis());//Only if You want to set min date
dp.show();

并将其添加到 MainClass

private final DatePickerDialog.OnDateSetListener preferredDate1Picker = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
        preferDate1 = new GregorianCalendar(selectedYear, selectedMonth, selectedDay);

        textview.setText((selectedDay < 10 ? "0" + String.valueOf(selectedDay) : String.valueOf(selectedDay)) + "/"
            + (selectedMonth + 1 < 10 ? "0" + String.valueOf((selectedMonth + 1)) : String.valueOf((selectedMonth + 1)))
            + "/" + selectedYear);

    }
};

推荐阅读