Вы имеете в виду, что хотите добавить виджет DatePicker в свои приложения.
Объявление глобальной переменной в вашем классе деятельности:
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
запишите этот код в функцию onCreate ():
//date picker presentation
mPickDate = (Button) findViewById(R.id.pickDate);//button for showing date picker dialog
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { showDialog(DATE_DIALOG_ID); }
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplay();
запишите эти функции вне функции onCreate ():
//return date picker dialog
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
//update month day year
private void updateDisplay() {
mBodyText.setText(//this is the edit text where you want to show the selected date
new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-")
.append(mMonth + 1).append("-")
.append(mDay).append(""));
//.append(mMonth + 1).append("-")
//.append(mDay).append("-")
//.append(mYear).append(" "));
}
// the call back received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};