如何在android中的datepicker对话框中设置最大date?
在我的应用程序使用dateselect器来设置date.i要设置dateselect器的最大date是作为今天date根据系统date.i不知道如何设置dateselect器最大date为今天date.Can任何一个知道帮助我解决这个问题。
我的dateselect器编码是:
private int pYear; private int pMonth; private int pDay; static final int DATE_DIALOG_ID = 0; final Calendar c = Calendar.getInstance(); pYear = c.get(Calendar.YEAR); pMonth = c.get(Calendar.MONTH); pDay = c.get(Calendar.DAY_OF_MONTH); // Date picker public Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { pYear = year; pMonth = monthOfYear; pDay = dayOfMonth; e_dob.setText(new StringBuilder() // to set date in editext .append(pDay).append("/").append(pMonth + 1) .append("/").append(pYear).append(" ")); } }; return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay); } return null; }
使用setMaxDate()
。
例如,replacereturn new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay)
语句像这样:
DatePickerDialog dialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay); dialog.getDatePicker().setMaxDate(new Date().getTime()); return dialog;
Calendar c = Calendar.getInstance(); c.set(2017, 0, 1);//Year,Mounth -1,Day your_date_picker.setMaxDate(c.getTimeInMillis());
我们也可以做到这一点
long now = System.currentTimeMillis() - 1000; dp_time.setMinDate(now); dp_time.setMaxDate(now+(1000*60*60*24*7)); //After 7 Days from Now
你可以尝试replace这一行:
return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
由那些:
DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay); DatePicker datePicker = dpDialog.getDatePicker(); Calendar calendar = Calendar.getInstance();//get the current day datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date return dpDialog;
private int pYear; private int pMonth; private int pDay; static final int DATE_DIALOG_ID = 0; /**inside oncreate */ final Calendar c = Calendar.getInstance(); pYear= c.get(Calendar.YEAR); pMonth = c.get(Calendar.MONTH); pDay = c.get(Calendar.DAY_OF_MONTH); @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, pYear, pMonth-1, pDay); } return null; } protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case DATE_DIALOG_ID: ((DatePickerDialog) dialog).updateDate(pYear, pMonth-1, pDay); break; } } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // write your code here to get the selected Date } };
试试这应该工作。 谢谢
看看我的自定义datepickerdialog
https://github.com/Redman1037/EightFoldsDatePickerDialog
import android.app.DatePickerDialog; import android.content.Context; import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; /** * Created by Manohar on 14/08/2017. */ public class EightFoldsDatePickerDialog extends DatePickerDialog { private DatePickerDialog datePickerDialog; private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); @RequiresApi(api = Build.VERSION_CODES.N) public EightFoldsDatePickerDialog(@NonNull Context context) { super(context); datePickerDialog = new DatePickerDialog(context); } @RequiresApi(api = Build.VERSION_CODES.N) public EightFoldsDatePickerDialog(@NonNull Context context, int themeResId) { super(context, themeResId); datePickerDialog = new DatePickerDialog(context, themeResId); } public EightFoldsDatePickerDialog(@NonNull Context context, @Nullable OnDateSetListener listener, int year, int month, int dayOfMonth) { super(context, listener, year, month, dayOfMonth); datePickerDialog = new DatePickerDialog(context, listener, year, month, dayOfMonth); } public EightFoldsDatePickerDialog(@NonNull Context context, int themeResId, @Nullable OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) { super(context, themeResId, listener, year, monthOfYear, dayOfMonth); datePickerDialog = new DatePickerDialog(context, themeResId, listener, year, monthOfYear, dayOfMonth); } public void show() { datePickerDialog.show(); } public void setMinDate(int year, int month, int day) { String minDate = "" + day + "-" + getmonth(month) + "-" + year; try { datePickerDialog.getDatePicker().setMinDate(dateFormat.parse(minDate).getTime()); } catch (ParseException e) { e.printStackTrace(); } } public void setMaxDate(int year, int month, int day) { String maxDate = "" + day + "-" + getmonth(month) + "-" + year; try { datePickerDialog.getDatePicker().setMaxDate(dateFormat.parse(maxDate).getTime()); } catch (ParseException e) { e.printStackTrace(); } } public void setTodayAsMaxDate() { datePickerDialog.getDatePicker().setMaxDate(Calendar.getInstance().getTime().getTime()); } public void setTodayAsMinDate() { datePickerDialog.getDatePicker().setMinDate(Calendar.getInstance().getTime().getTime()); } private String getmonth(int month) { switch (month) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } return "January"; } }
用法
将EightFoldsDatePickerDialog.java复制到您的项目中
EightFoldsDatePickerDialog datePickerDialog=new EightFoldsDatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { //Do something with date Toast.makeText(this, "Year "+year+" month "+month+" day "+dayOfMonth, Toast.LENGTH_SHORT).show(); } },year,month,day); // give any year , month , day values, this will be opened by default in dialog datePickerDialog.setMinDate(2017,8,7); //arguments are year , month , date (use for setting custom min date) datePickerDialog.setMaxDate(2017,8,25); //arguments are year , month , date (use for setting custom max date) // datePickerDialog.setTodayAsMinDate(); // sets today's date as min date // datePickerDialog.setTodayAsMaxDate(); // sets today's date as max date datePickerDialog.show();
结果
尝试这个
我已经尝试了太多的解决scheme,但他们都没有工作,浪费了我半天后,我终于做出了解决scheme。
此代码只是简单地向您展示DatePickerDialog,包括最小和最大date,月份和年份,无论您想要修改它。
final Calendar calendar = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker arg0, int year, int month, int day_of_month) { calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, (month+1)); calendar.set(Calendar.DAY_OF_MONTH, day_of_month); String myFormat = "dd/MM/yyyy"; SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.getDefault()); your_edittext.setText(sdf.format(calendar.getTime())); } },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());// TODO: used to hide previous date,month and year calendar.add(Calendar.YEAR, 0); dialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());// TODO: used to hide future date,month and year dialog.show();
输出: –禁用以前和将来的日历
您可以创build自定义dateselect器 ,即适用于所有api级别。
public class CustomDatePickerDialog extends DatePickerDialog { int maxYear; int maxMonth; int maxDay; public CustomDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { super(context, callBack, year, monthOfYear, dayOfMonth); } public void setMaxDate(long maxDate) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { getDatePicker().setMaxDate(System.currentTimeMillis()); } else { final Calendar c = Calendar.getInstance(); c.setTimeInMillis(maxDate); maxYear = c.get(Calendar.YEAR); maxMonth = c.get(Calendar.MONTH); maxDay = c.get(Calendar.DAY_OF_MONTH); } } @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { super.onDateChanged(view, year, monthOfYear, dayOfMonth); } else { if (year > maxYear) view.updateDate(maxYear, maxMonth, maxDay); if (monthOfYear > maxMonth && year == maxYear) view.updateDate(maxYear, maxMonth, maxDay); if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth) view.updateDate(maxYear, maxMonth, maxDay); } }
设置最大date
final CustomDatePickerDialog pickerDialog = new CustomDatePickerDialog(getActivity(), myDateListener, year, month, day); pickerDialog.setMaxDate(System.currentTimeMillis()); pickerDialog.show();
我通过以下解决这个问题
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 = new DatePickerDialog(getActivity(), this, year, month, day); datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
希望它会帮助你
DatePicker dp = (DatePicker) findViewById(R.id.datePicker1); dp.setMaxDate(new Date().getTime());