我在编辑文本上应用以下代码来选择日期。
public class DatePickerFragment extends DialogFragment
{
private OnDateSetListener listener;
public DatePickerFragment(OnDateSetListener listener)
{
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), listener, year, month, day);
}
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
//do some stuff
}
void showDateDialog()
{
DatePickerFragment newFragment = new DatePickerFragment(this);
newFragment.show(getFragmentManager(), "date_picker");
}
但是,我希望灰显所有过去的日期,以便不允许用户选择任何日期。这可以通过代码吗?或者我应该在选择后手动检查日期是否是过去的日期?
答案 0 :(得分:11)
使用setMinDate()功能。它设置自getDefault()时区1970年1月1日00:00:00以来的最小日期(以毫秒为单位)。将最短日期设置为今天的日期,因此用户将无法通过它。像onCreateDialog
中的关注一样:
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog d = new DatePickerDialog(getActivity(), listener, year, month, day);
DatePicker dp = d.getDatePicker();
dp.setMinDate(c.getTimeInMillis());
return d;
答案 1 :(得分:1)
date button on clcik method
dateButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
for date piker create dialog method :
case DATE_DIALOG_ID:
{
Calendar calendar = Calendar.getInstance();
final DatePickerDialog dojDPDilog = new DatePickerDialog(this,datePickerListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));
dojDPDilog.setTitle("Select Date");
try {
java.lang.reflect.Field mDatePickerField = dojDPDilog.getClass().getDeclaredField("mDatePicker");
mDatePickerField.setAccessible(true);
try {
dojDPicker = (DatePicker) mDatePickerField.get(dojDPDilog);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
dojDPicker.init(minYear, minMonth, minDay,new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
disableDate(view, year, monthOfYear, dayOfMonth);
}
});
return dojDPDilog;
}
for disable method:
private void disableDate(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
if (year > maxYear)
{
view.updateDate(maxYear, maxMonth, maxDay);
}
else if (monthOfYear > maxMonth && year == maxYear)
{
view.updateDate(maxYear, maxMonth, maxDay);
}
else if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth)
{
view.updateDate(maxYear, maxMonth, maxDay);
}
else if (year < minYear)
{
view.updateDate(minYear, monthOfYear, dayOfMonth);
}
else if (monthOfYear < minMonth && year == minYear)
{
view.updateDate(minYear, minMonth, dayOfMonth);
}
else if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth)
{
view.updateDate(minYear, minMonth, minDay);
}
}
and take local variables:
int maxYear = c.get(Calendar.YEAR) + 100;
int maxMonth = c.get(Calendar.MONTH);
int maxDay = c.get(Calendar.DAY_OF_MONTH);
int minYear = c.get(Calendar.YEAR);
int minMonth = c.get(Calendar.MONTH);
int minDay = c.get(Calendar.DAY_OF_MONTH);
答案 2 :(得分:0)
使用DatePicker.setMaxDate()
和DatePicker.setMinDate()
设置固定年份。
例如
Calendar c_max = Calendar.getInstance(),
Calendar c_min = Calendar.getInstance();
c_min.set(2012,0,1); // your min date boundary
c_max.set(2013,11,31); // your max date boundary
DatePicker.setMaxDate(c_max.getTimeInMillis());
DatePicker.setMinDate(c_min.getTimeInMillis());
从API 11添加setMaxDate()
和setMinDate()
后,您可以按照以下版本对其进行操作。
您可以使用init datePicker方法设置范围。
// Calendar
this.calendar = new GregorianCalendar();
this.datePicker = (DatePicker) findViewById(R.id.your_datepicker_id);
// check for the build version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// (picker is a DatePicker- set current time as min time.
this.datePicker.setMinDate(this.calendar.getTimeInMillis());
} else {
final int minYear = this.calendar.get(Calendar.YEAR);
final int minMonth = this.calendar.get(Calendar.MONTH);
final int minDay = this.calendar.get(Calendar.DAY_OF_MONTH);
this.datePicker.init(minYear, minMonth, minDay,
new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int month, int day) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, month, day);
if (calendar.after(newDate)) {
view.init(minYear, minMonth, minDay, this);
}
}
});
}
答案 3 :(得分:0)
嘿,在onDateSet()方法中尝试这个。这适用于片段。
Calendar calendar1 = Calendar.getInstance();
calendar1.set(2014,11-1,11);
view.setMinDate(calendar1.getTimeInMillis()-10000);