android - 如何在我的应用程序中设置我的 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));
此代码仅在我保存笔记时显示日期,它不允许我为我的活动选择日期
解决方案
您可以使用以下方式显示 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);
}
};
推荐阅读
- react-native - 在 React Native 中从 Fire 库中读取特定值
- php - php mysqli 获取数据并将两个变量除以固定(2)/两位小数
- javascript - Heroku 错误:找不到模块“优化器”导致内部服务器错误
- javascript - 如何使用 VueJS 获取 json Web API 的值
- webhooks - Postmates API 发送订购详情
- java - 当位置设置 OUTSIDE_CHART 和 XAxis 也不显示 OUTSIDE 时,YAxis 不显示
- angular - 具有非静态默认值的默认参数是否合法?
- c - 阻塞模式下的 ALSA 'snd_pcm_writei' 行为
- python - 为什么我会收到“TypeError:必须是 str,而不是 float”错误。我试图转换它
- javascript - 是否可以链接价值设定者?