3

Previously i was using showDialog() which now is deprecated.

I am already in a Fragment which contains an EditText. How can i call this DatePickerDialog and set the EditText's Text to the selected date?

Using DialogFragment seems a bit complicated.

Thank You

4

2 回答 2

14

这很简单:

DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year,
            int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};

DatePickerDialog d = new DatePickerDialog(getActivity(),
        R.style.MyThemee, mDateSetListener, mYear, mMonth, mDay);
d.show();

更新显示方法:

private void updateDisplay() {

    GregorianCalendar c = new GregorianCalendar(mYear, mMonth, mDay);
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");

    editDate.setText(sdf.format(c.getTime()));

    sdf = new SimpleDateFormat("yyyy-MM-dd");

    transDateString=sdf.format(c.getTime());
}// updateDisplay
于 2012-10-26T07:54:09.837 回答
0

请查看 android API 指南:http: //developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog

于 2012-10-26T07:56:04.813 回答