Android: get DatePickerDialog in a fragment

依然范特西╮ 提交于 2020-01-02 08:36:09

问题


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


回答1:


Its easy:

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

UpdateDisplay method:

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



回答2:


Please take a look at android API guides : http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog



来源:https://stackoverflow.com/questions/13082861/android-get-datepickerdialog-in-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!