一个对话框中的安卓日期时间选择器

我正在为我的Android应用程序使用材料选择器。但我想将和选取器组合在一个对话框中。DateTimeDateTime

我正在关注这个。但是我需要在选择日期之后选择时间。

请建议可能的方法来做到这一点。


答案 1

您可以在设置日期后呼叫。喜欢这个。。TimePickerDatePicker

    String date_time = "";
    int mYear;
    int mMonth;
    int mDay;

    int mHour;
    int mMinute;

    et_show_date_time = (EditText) findViewById(R.id.et_show_date_time);
    btn_set_date_time = (Button) findViewById(R.id.btn_set_date_time);

    btn_set_date_time.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            datePicker();

        }
    });

日期选取器:

    private void datePicker(){

    // Get Current Date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {

                    date_time = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
                   //*************Call Time Picker Here ********************
                   tiemPicker();
                }
            }, mYear, mMonth, mDay);
       datePickerDialog.show();
    }

时间选取器

 private void tiemPicker(){
    // Get Current Time
    final Calendar c = Calendar.getInstance();
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);

    // Launch Time Picker Dialog
    TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay,int minute) {

                    mHour = hourOfDay;
                    mMinute = minute;

                    et_show_date_time.setText(date_time+" "+hourOfDay + ":" + minute);
                }
            }, mHour, mMinute, false);
    timePickerDialog.show();
}

--


答案 2

科特林

下面是经过测试的 Kotlin 代码,用于将 和 组合在一起。由于 Kotlin 中闭包的支持,它变得更加简单。以下函数应放在 您的 或 中。该方法是 的成员。如果您使用的是 ,请改用 。DatePickerDialogTimePickerDialogFragmentActivityrequireContext()FragmentActivityapplicationContext

private fun pickDateTime() {
    val currentDateTime = Calendar.getInstance()
    val startYear = currentDateTime.get(Calendar.YEAR)
    val startMonth = currentDateTime.get(Calendar.MONTH)
    val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH)
    val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY)
    val startMinute = currentDateTime.get(Calendar.MINUTE)

    DatePickerDialog(requireContext(), DatePickerDialog.OnDateSetListener { _, year, month, day ->
        TimePickerDialog(requireContext(), TimePickerDialog.OnTimeSetListener { _, hour, minute ->
            val pickedDateTime = Calendar.getInstance()
            pickedDateTime.set(year, month, day, hour, minute)
            doSomethingWith(pickedDateTime)
        }, startHour, startMinute, false).show()
    }, startYear, startMonth, startDay).show()
}

在按钮中调用上述函数,如下所示:onClickListener

button.setOnClickListener { pickDateTime() }

就是这样!

如果要根据用户的设置以 12 小时或 24 小时格式显示时钟,请替换为falseDateFormat.is24HourFormat(requireContext())


推荐