Android DatePicker:根据当前日期禁用以前的日期
Posted
技术标签:
【中文标题】Android DatePicker:根据当前日期禁用以前的日期【英文标题】:Android DatePicker : Disabling the PREVIOUS dates based on the CURRENT date 【发布时间】:2014-04-25 04:07:18 【问题描述】:我的应用中有一个DatePicker
,它是这样设置的:
布局:
<TextView
android:id="@+id/traveltext"
android:layout_
android:layout_
android:layout_weight="1.11"
android:text="Date Of Travel"
android:textStyle="bold" />
<EditText
android:id="@+id/txtDate"
android:layout_
android:layout_
android:layout_weight="1" >
</EditText>
<Button
android:id="@+id/btnCalendar"
android:layout_
android:layout_
android:layout_marginRight="50dp"
android:background="@drawable/date" >
</Button>
</LinearLayout>
全球价值观:
Button btnCalendar;
EditText txtDate;
private int mYear, mMonth, mDay;
创建时:
btnCalendar = (Button) findViewById(R.id.btnCalendar);
txtDate = (EditText) findViewById(R.id.txtDate);
btnCalendar.setOnClickListener(this);
方法:
@Override
public void onClick(View v)
if (v == btnCalendar)
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
// Display Selected date in textbox
txtDate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
, mYear, mMonth, mDay);
dpd.show();
现在我的问题是,如何根据当前日期禁用以前的日期?
请帮忙。谢谢。
【问题讨论】:
【参考方案1】:private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
// the callback received when the user "sets" the Date in the
// DatePickerDialog
public void onDateSet(DatePicker view, int yearSelected,
int monthOfYear, int dayOfMonth)
year = yearSelected;
month = monthOfYear + 1;
day = dayOfMonth;
// Set the Selected Date in Select date Button
txtarrivedate.setText(year + "-" + month + "-" + day);
;
// 当你调用showDialog()
方法时,方法会自动被调用
@Override
protected Dialog onCreateDialog(int id)
switch (id)
case 1:
// create a new DatePickerDialog with values you want to show
DatePickerDialog da = new DatePickerDialog(this, mDateSetListener,
mYear, mMonth, mDay);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date newDate = c.getTime();
da.getDatePicker().setMinDate(newDate.getTime());
return da;
// create a new TimePickerDialog with values you want to show
case 2:
// create a new DatePickerDialog with values you want to show
DatePickerDialog da1 = new DatePickerDialog(this,
mDateSetListener2, mYear, mMonth, mDay);
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DATE, 1);
Date newDate2 = c1.getTime();
da1.getDatePicker().setMinDate(newDate2.getTime());
return da1;
return null;
仅使用用于调用对话框 显示对话框(1);
【讨论】:
但它显示IllegalArgumentException
为这一行:dpd.getDatePicker().setMinDate(newDate2.getTime());
dpd.getDatePicker().setMinDate(System.currentTimeMillis());
很好,但是如果我将onDateSet
method 放在onClick 按钮内,为什么它不能识别mDateSetListener
?我该如何解决这个问题?
是的,现在我有一个小问题:当我在手机上安装它时有什么问题?我的意思是选择了不应该发生的以前的日期。
你的手机是哪个android os版本??【参考方案2】:
试试这个解决方案:
dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
【讨论】:
【参考方案3】:试试这个:
// set date picker with current date
DatePickerDialog date = new DatePickerDialog(this, datePickerListener, mYear,mMonth,mDay)
// Comapre the date selected in picker with the current date.
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
if (year < mYear)
view.updateDate(mYear,mMonth,mDay);
if (monthOfYear < mMonth && year == mYear)
view.updateDate(mYear,mMonth,mDay);
if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
view.updateDate(mYear,mMonth,mDay);
;
return date;
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener()
// as soon as the dialog box is closed, this method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay)
mYear = selectedYear;
mMonth = selectedMonth;
mDay = selectedDay;
// set selected date into textview
txtDate.setText(new StringBuilder().append(mMonth + 1)
.append("-").append(mDay).append("-").append(mYear)
.append(" "));
;
【讨论】:
好的,伙计,但是一旦我单击按钮,即 btnCalendar(在布局中查看)DatePickerDialog 不会弹出,那么我应该将 onClick 放在哪里?【参考方案4】:我认为您实际上不能禁用日期选择器。但是一旦选择了日期,您可以使用当前日期验证它并让用户重新选择未来的日期。如果用户选择过去的日期,您可以发送祝酒消息。
【讨论】:
日期选择器只是另一种生成日期的选项,您可以从编辑文本字段中获取它。所以我个人认为这将是一种更简单的做事方式。 好的,如果是这样,那么请帮助我用当前日期验证所选日期。我该怎么做? 可以通过 Calendar.getInstance(); 获取当前日期;【参考方案5】:在您的 onDateSet 中,您可以验证日期,就像您只想在今天的日期之后设置日期,然后在 onDateSet 方法中添加以下代码
Calendar cl = Calendar.getInstance();
cl.set(year, monthOfYear, dayOfMonth);
if (cl.after(Calendar.getInstance()))
//set your date
else
//do nothing
希望这对你有用
【讨论】:
【参考方案6】:etDateOfDelivery.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
new DatePickerDialog(SendOrderActivity.this,
new OnDateSetListener()
@Override
public void onDateSet(DatePicker arg0, int year,
int month, int day)
if(month==intMonthOfYear && day<intDayOfMonth &&year<=intYear)
// your message. dont use dialogmessage. its my custom dialog. use in your away
DialogMessage.customDialogSingleButton(context, "Invalid Date", "Please Set Comming Date", "Cancel");
else if(month<=intMonthOfYear && day==intDayOfMonth && year<=intYear)
DialogMessage.customDialogSingleButton(context, "Invalid Date", "Please Set Comming Date", "Cancel");
else if(month<=intMonthOfYear && day<=intDayOfMonth && year<intYear)
DialogMessage.customDialogSingleButton(context, "Invalid Date", "Please Set Comming Date", "Cancel");
else
if(month<9)
etDateOfDelivery.setText("0"+(month + 1) + "/"
+ day+ "/" + year);
else
etDateOfDelivery.setText((month + 1) + "/"
+ day+ "/" + year);
, intYear, intMonthOfYear, intDayOfMonth).show();
);
【讨论】:
【参考方案7】:我设置了一个名为 startbtn 的按钮和侦听器,这样每当用户单击该按钮时,它都会提示一个日期选择器对话框并将日期设置为一个名为 startet 的 TextView
//getting calendar instance
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
startbtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
DatePickerDialog startDatePicker = new DatePickerDialog(Search.this, new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
startet.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
startDay = dayOfMonth;
startMonth = month + 1;
startYear = year;
, year, month, day);
//Disable date before today
startDatePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
startDatePicker.show();
);
【讨论】:
以上是关于Android DatePicker:根据当前日期禁用以前的日期的主要内容,如果未能解决你的问题,请参考以下文章
android 日期控件 datePicker 如何去掉右边的日历表
如何在 android 的 datepicker 对话框中设置最大日期?