可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a DatePicker
in my app which is set in this way :
Layout :
<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>
Global Values :
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);
Method :
@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();
} }
Now my question is, how do i disable the PREVIOUS dates based on the CURRENT date?
Please help. Thanks.
回答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);
}
};
// Method automatically gets Called when you call showDialog()
method
@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;
}
only use use the for calling dialog
showDialog(1);
回答2:
Try this:
// 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(" "));
}
};
回答3:
Simply try this solution:
dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
回答4:
I don't think you can actually disable the date picker. But once the date is selected, you can validate it with current date and make the user reselect future date. You can send a toast message if the user selects the past date.
回答5:
In your onDateSet you can validation on date like you want to set date only after today's date then add the following code in onDateSet method
Calendar cl = Calendar.getInstance();
cl.set(year, monthOfYear, dayOfMonth);
if (cl.after(Calendar.getInstance())) {
//set your date
}else{
//do nothing
}
hope this will work for you
回答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();
}
});