如何在datepickerdialog android中设置焦点到最小日期?

时间:2017-09-22 06:00:08

标签: android datepickerdialog

在DatePickerDialog中设置最小和最大日期后,无法将焦点设置为最小日期。

final DatePickerDialog datePickerDialog = new DatePickerDialog(Test.this, new DatePickerDialog.OnDateSetListener() {
  @Override
  public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {   
  }
}, mYear, mMonth, mDay);
Calendar max= Calendar.getInstance();
max.add(Calendar.DAY_OF_MONTH, 4);

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.getDatePicker().setMaxDate(max.getTimeInMillis());
datePickerDialog.show();`

1 个答案:

答案 0 :(得分:0)

这是今天设置为最小日期的代码,最长日期是4年后。 在今天之前和4年之后,您将无法选择日期。

public class DateActivity extends Activity {

TextView tvDatePicker;
Calendar cal,cal1;
long maxDate;
Date date;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cal = Calendar.getInstance();
    cal1 = Calendar.getInstance();

    tvDatePicker = (TextView) findViewById(R.id.txtDatePicker);
    tvDatePicker.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {

                }
        }, cal.YEAR, cal.MONTH, cal.DAY_OF_MONTH);
        dialog.getDatePicker().setMinDate(System.currentTimeMillis());
        cal1.add(Calendar.YEAR, 4);
        dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
        dialog.show();
    }
});
}