2016-01-13 56 views
0

我使用這個鏈接Date Picker設定分鐘日期在日期選擇器使用對話框片段

我知道我們可以用setMinDate()方法設定最低日期創建我的日期選擇器創建的,但我不明白怎麼做如果我們按照鏈接中提供的方式執行日期選擇器。 我沒有找到任何使用此方法的方法。 日期選擇器代碼

public static class DatePickerFragment extends DialogFragment 
     implements DatePickerDialog.OnDateSetListener { 

    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current date as the default date in the picker 
     tv_date= (TextView)getActivity().findViewById(R.id.tv_date); 
     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); 

     // Create a new instance of DatePickerDialog and return it 
     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    public void onDateSet(DatePicker view, int year, int month, int day) { 
     // Do something with the date chosen by the user 
     tv_date.setText(new StringBuilder().append(day).append("-") 
       .append(month+1).append("-").append(year)); 
     date=tv_date.getText().toString(); 
    } 
} 
+0

http://stackoverflow.com/questions/16749361/how-set-maximum-date-in-datepicker-dialog-in-android – Raghunandan

+0

@Raghunandan首先檢查鏈接我提供。實現方式與我的情況不同 –

+0

Look @ http://developer.android.com/reference/android/app/DatePickerDialog.html#getDatePicker()。在onCreateDialog中,你返回一個日期選擇器對話框 – Raghunandan

回答

1

正如你可以在這個鏈接查看:DatePicker#setMinDate,我們需要獲取日期選取對象設置最小或最大日期選取器對話框。

請參見下面給出的代碼片段,這將解決您的問題:

public static class DatePickerFragment extends DialogFragment 
          implements DatePickerDialog.OnDateSetListener { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current date as the default date in the picker 
     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); 

     // Create a new instance of DatePickerDialog and return it 
     DatePickerDialog dialog = new DatePickerDialog(context, this, year, month, day); 

     //set min date (ex. current date and time) - pass your custom date and time in milliseconds 
     dialog.getDatePicker().setMinDate(c.getTimeInMillis()); 
     //set max date 
     //dialog.getDatePicker().setMaxDate(maxDate); 

     return dialog; 
    } 

    public void onDateSet(DatePicker view, int year, int month, int day) { 
     // Do something with the date chosen by the user 
    } 
} 

希望它會幫助你。

1

使用此代碼:

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; 
+0

仔細閱讀問題,然後回答這個問題 –

相關問題