我的应用中有DatePicker
以这种方式设置:
布局:
<TextView
android:id="@+id/traveltext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.11"
android:text="Date Of Travel"
android:textStyle="bold" />
<EditText
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
<Button
android:id="@+id/btnCalendar"
android:layout_width="30dp"
android:layout_height="42dp"
android:layout_marginRight="50dp"
android:background="@drawable/date" >
</Button>
</LinearLayout>
全球价值观:
Button btnCalendar;
EditText txtDate;
private int mYear, mMonth, mDay;
OnCreate:
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();
} }
现在我的问题是,如何根据CURRENT日期禁用PREVIOUS日期?
请帮忙。感谢。
答案 0 :(得分:7)
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;
}
仅使用用于调用对话框 的ShowDialog(1);
答案 1 :(得分:1)
试试这个:
// 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(" "));
}
};
答案 2 :(得分:1)
只需尝试此解决方案:
dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
答案 3 :(得分:0)
我认为您实际上无法禁用日期选择器。但是一旦选择了日期,您就可以使用当前日期对其进行验证,并让用户重新选择将来的日期。如果用户选择过去的日期,您可以发送Toast消息。
答案 4 :(得分:0)
在你的onDateSet中你可以在日期验证,就像你想在今天的日期之后设置日期一样,然后在onDateSet方法中添加以下代码
Calendar cl = Calendar.getInstance();
cl.set(year, monthOfYear, dayOfMonth);
if (cl.after(Calendar.getInstance())) {
//set your date
}else{
//do nothing
}
希望这对你有用
答案 5 :(得分:0)
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();
}
});
答案 6 :(得分:0)
我已经设置了一个名为startbtn和listener的按钮,以便每当用户单击该按钮时,它将提示一个日期选择器对话框,并将日期设置为一个名为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();
}
});