选择日期后,日期选择器应在 android 中关闭,而无需单击确定按钮

Posted

技术标签:

【中文标题】选择日期后,日期选择器应在 android 中关闭,而无需单击确定按钮【英文标题】:After Selecting date the date picker should close in android without clicking okay button 【发布时间】:2016-11-29 21:20:00 【问题描述】:

我在 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();
 

【问题讨论】:

【参考方案1】:

@OverrideonDateChangeddismiss(); 它。

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。

【讨论】:

【参考方案2】:
class CustomDatePickerDialog(
        context: Context,
        private val calendar: Calendar,
        private val callback: (newTimestamp: Long) -> Unit
) : DatePickerDialog(context, createListener(calendar, callback), calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH]) 

    override fun onDateChanged(view: DatePicker, year: Int, month: Int, dayOfMonth: Int) 
        super.onDateChanged(view, year, month, dayOfMonth)
        dateChangeHandler(year, month, dayOfMonth, calendar, callback)
        dismiss()
    

    companion object 
        fun dateChangeHandler(year: Int, month: Int, day: Int, calendar: Calendar, callback: (newTimestamp: Long) -> Unit) 
            calendar[year, month, day, 0, 0] = 0
            callback(calendar.timeInMillis)
        

        fun createListener(calendar: Calendar, callback: (newTimestamp: Long) -> Unit) = OnDateSetListener  _: DatePicker?, i: Int, i1: Int, i2: Int ->
            dateChangeHandler(i, i1, i2, calendar, callback)
        
    



fun Context.calendarOnClick(currentDate: Long?, callback: (newTimestamp: Long) -> Unit) 
    val calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"))
    currentDate?.let  calendar.timeInMillis = it 
    CustomDatePickerDialog(this, calendar, callback).show()

【讨论】:

以上是关于选择日期后,日期选择器应在 android 中关闭,而无需单击确定按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何设置开始日期和结束日期之间的验证应在 3 年内选择

什么时候在Android中关闭光标?

jQuery UI Datepicker - 选定日期之后的颜色日期

android中关于时间的控件

Android:应用程序在调试过程中关闭

如何在 IntelliJ 中关闭鼠标块选择?