Android的:在一个片段让DatePickerDialog
问题描述:
previously我使用的的ShowDialog()这是现在pcated德$ P $。
Previously i was using showDialog() which now is deprecated.
我已经在片段,其中包含一个EditText。我怎么能称之为 DatePickerDialog 和EditText上的文本设置为选定的日期?
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?
使用的 DialogFragment 似乎有点复杂。
感谢您
答
它很容易:
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方式:
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