2016-07-26 76 views
3

我正在使用Android的默認日期選擇器,現在我想關閉對話框 選擇日期後,日期選擇器應該關閉在Android沒有點擊好的按鈕選擇日期後,日期選擇器應該關閉在Android沒有點擊好的按鈕

private void updateDOB() { 
    String myFormat = "yyyy/MM/dd"; //In which you need put here 
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); 
      btnDOB.setText(sdf.format(calendar.getTime())); 
      tvDOB.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)); 
} 

private void selectDOB() { 
    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { 
     @Override 
     public void onDateSet(DatePicker datePicker, int year, int month, int day) { 
      calendar.set(Calendar.YEAR, year); 
      calendar.set(Calendar.MONTH, month); 
      calendar.set(Calendar.DAY_OF_MONTH, day); 
      updateDOB(); 
     } 
    }; 
    new DatePickerDialog(ActivityAddChild.this, listener, 
      calendar.get(Calendar.YEAR), 
      calendar.get(Calendar.MONTH), 
      calendar.get(Calendar.DAY_OF_MONTH)).show(); 
} 

回答

2

@OverrideonDateChanged和​​它。

new DatePickerDialog(context, null, year, month, day) { 
    @Override 
    public void onDateChanged(@NonNull DatePicker view, int year, int month, int dayOfMonth) { 
     /* Here you got the date without "OnDateSetListener" */ 

     dismiss(); 
    } 
}.show(); 

引用自this答案。

請注意,這不適用於Android 5.0,see there

改爲使用MaterialDateTimePicker

相關問題