如何打开日期选择器对话框?

时间:2015-08-24 17:16:56

标签: android datepicker dialog android-datepicker datepickerdialog

如何打开日期选择器对话框?

我尝试使用此代码,但这并没有做任何事情。我相信它已被弃用,但我还没有找到解决方案。

mDOB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);

        }
    });

1 个答案:

答案 0 :(得分:1)

class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            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);

            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // get month, year and day
        }
    }

并使用此方法调用此对话框...

new DatePickerFragment().show(getFragmentManager(), "datepicker");