如何在 Android 日期选择器中禁用未来日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20970963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Disable future dates in Android date picker
提问by venu
How to Disable future dates in Android date picker
如何在 Android 日期选择器中禁用未来日期
Java Code :
代码:
mExpireDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// To show current date in the datepicker
final Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(
EventRegisterActivity.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker,
int selectedyear, int selectedmonth,
int selectedday) {
mcurrentDate.set(Calendar.YEAR, selectedyear);
mcurrentDate.set(Calendar.MONTH, selectedmonth);
mcurrentDate.set(Calendar.DAY_OF_MONTH,
selectedday);
SimpleDateFormat sdf = new SimpleDateFormat(
getResources().getString(
R.string.date_card_formate),
Locale.US);
mExpireDate.setText(sdf.format(mcurrentDate
.getTime()));
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle(getResources().getString(
R.string.alert_date_select));
mDatePicker.show();
}
});
How to do it?
怎么做?
回答by laalto
Get the DatePicker
from DatePickerDialog
with getDatePicker()
. Set the max date to current date with setMaxDate()
:
DatePicker
从DatePickerDialog
with 中获取getDatePicker()
。使用以下命令将最大日期设置为当前日期setMaxDate()
:
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
Requires API level 11.
需要 API 级别 11。
回答by Satyaki Mukherjee
You can call getDatePicker().setMaxDate(long) on your DatePickerDialog to set today as your maximum date. You can update the function with the same name from the snippet you posted.
您可以在 DatePickerDialog 上调用 getDatePicker().setMaxDate(long) 以将今天设置为您的最大日期。您可以从您发布的代码段中使用相同的名称更新函数。
Note:: DatePickerDialog is the object that I referenced in the Android Docs from the link I posted.
注意:: DatePickerDialog 是我在 Android 文档中从我发布的链接中引用的对象。
@Override
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
//start changes...
DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
return dialog;
//end changes...
}
return null;
}
Try this and give your feedback!!!
试试这个并提供您的反馈!!!
回答by Yashoda Bane
Following code help you to disable future dates:
Declare calendar variable globally:
以下代码可帮助您禁用未来日期:
全局声明日历变量:
private Calendar myCalendar = Calendar.getInstance();
Put following code in onCreate method:
将以下代码放入 onCreate 方法中:
DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
On the button click put the following code:
在按钮上单击放置以下代码:
DatePickerDialog datePickerDialog=new DatePickerDialog(getActivity(), dateListener, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
//following line to restrict future date selection
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
datePickerDialog.show();
回答by Ketan Ahir
If user select future date then update datepicker to current date(today)
如果用户选择未来日期,则将日期选择器更新为当前日期(今天)
you can use following code to check selected is future date or not
您可以使用以下代码检查所选日期是否为未来日期
final Calendar cal = Calendar.getInstance();
datePickerDob.init(currentYear, currentMonth, currentDay,
new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar selectedCal = Calendar.getInstance();
selectedCal.set(year, monthOfYear, dayOfMonth);
long selectedMilli = selectedCal.getTimeInMillis();
Date datePickerDate = new Date(selectedMilli);
if (datePickerDate.after(new Date())) {
datePickerDob.updateDate(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
} else {
}
}
});
You can also use compareTo() method
您还可以使用 compareTo() 方法
datePickerDate.compareTo(new Date());
Compare the receiver to the specified Date to determine the relative ordering.
将接收器与指定的日期进行比较以确定相对顺序。
Parameters date a Date to compare against.
参数 date 一个要比较的日期。
Returns an int < 0 if this Date is less than the specified Date, 0 if they are equal, and an int > 0 if this Date is greater.
如果此日期小于指定的日期,则返回 int < 0,如果它们相等,则返回 0,如果此日期更大,则返回 int > 0。
回答by Ph?m Hùng
If you using material design Datepicker :
如果您使用材料设计 Datepicker :
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(this,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show(getFragmentManager(), "datepicker");
datePickerDialog.setMaxDate(calendar);
回答by saurabh kumbhar
// Custom Your Future Dates
// Example for today and next 3 days :- 3(NUMBER OF NEXT DAYS)*24*60*60*1000l
// As 24 represents hrs in day
// As 60 mins 60 secs and convert it to millisec
//Inside Class which implements DatePickerDialog.OnDateSetListener
private Calendar mCurrentDate;
//Inside OnCreate Method
mDateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentDate = Calendar.getInstance();
int year = mCurrentDate.get(Calendar.YEAR);
int month = mCurrentDate.get(Calendar.MONTH);
int day = mCurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePickerDialog = new DatePickerDialog(this, this, year, month, day);
mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + 3 * 24 * 60 * 60 * 1000 l);
}
});
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mDateEditText.setText(dayOfMonth + "/" + month + "/" + year);
}
回答by Varun
Set the maximum date till tomorrow The last digit 1 in the code represents the number of days you want to set it.
将最大日期设置到明天代码中的最后一位数字 1 表示您要设置的天数。
pickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + (1000 * 60 * 60 * 24 * 1));
回答by Keshav Gera
**new Way**
ed_date.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
int month_k=selectedmonth+1;
}
},year, month, day);
mDatePicker.setTitle("Please select date");
// TODO Hide Future Date Here
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
// TODO Hide Past Date Here
// mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
mDatePicker.show();
}
});
回答by Armando Esparza García
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(), ondateSet, year, month, day);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date newDate = c.getTime();
dialog.getDatePicker().setMaxDate(newDate.getTime() - (newDate.getTime() % (24 * 60 * 60 * 1000)));
return dialog;
}