如何使用Android的DatePicker小部件设置今天的日期或未来日期?

时间:2015-08-05 03:10:02

标签: android android-dialogfragment android-calendar android-datepicker

我正在DatePicker中加载DialogFragment。我希望用户只能选择今天的日期或未来的日期。使用setMinDate()的各种方法显示不正确的输出或崩溃应用程序。例如,下面的内容不起作用。

dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

请告知。

DatePickerFragment.java

    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    public DatePickerFragment() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    final  Calendar cal = Calendar.getInstance();
    int currentyear = cal.get(Calendar.YEAR);
    int currentmonth = cal.get(Calendar.MONTH);
    int currentday = cal.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,currentyear,currentmonth,currentday);
    dialog.getDatePicker().setCalendarViewShown(true);
    dialog.getDatePicker().setSpinnersShown(false);
    dialog.setTitle("Pick a Due Date");

    return dialog;
    }


    public void onDateSet(DatePicker view, int year, int month, int day) {
    EditText txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
    String date=(month+1) + "/" + day + "/" + year;
    txtDate.setText(date);
    }

    ...

0 个答案:

没有答案