android - 如何禁用当前日期之后的日期?
问题描述
帮助在日期选择器中禁用从当前日期开始的所有日期。
这是我的代码:
public class MyDatePickerDialogue extends DatePickerDialog{
private CharSequence title;
private Context context;
public MyDatePickerDialogue(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
this.context = context;
}
public void setPermanentTitle(CharSequence title) {
this.title = title;
setTitle(title);
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
super.onDateChanged(view, year, month, day);
setTitle(title);
Toast.makeText( context,"Date picked", Toast.LENGTH_SHORT).show();
SingletonClass.getSingletonClass().singletondate = year+"-"+(month+1)+"-"+day;
//save the date that is picked into the singleton class
//i represents year , i1 represents month and i2 represents day
context.startActivity(new Intent(context,DescriptionActivity.class));
}
}
解决方案
您可以为 DatePicker 视图设置 maxDate
Date today = new Date();
Calendar c = Calendar.getInstance(TimeZone.getDefault());
c.setTime(today);
maxDate = c.getTime().getTime();
view.setMaxDate(maxDate);
推荐阅读
- php - 当hasMany关系为空时,Laravel如何返回值NULL
- xml - 使用 Powershell 解析 XML 文件
- python - 在 Python 中使用频率表
- perl - 如何实现对 Perl 的 HTTP::DAV 的 https 支持
- intellij-idea - Cucumber 功能无法识别 IntelliJ 中的步骤
- mysql - 当我无法发现问题时,如何解决 SQL ERROR 1604 语法错误?
- python - Discord.py if 语句表情符号不继续
- html - 如何在css中移动元素
- c# - 使 ASP.NET MVC 和 ASP.NET CORE 中的身份提供者共存
- r - 在 R 中创建一个 model.matrix 的定量预测器的问题