我想將DatePickerDialog
的值設置爲EditText
與(sdf.format(myCalendar.getTime()));
。我試過這種方式,但它不起作用。如何解決它?如何將DatePickerDialog值設置爲EditText?
下面是我的代碼:
Calendar myCalendar;
DatePickerDialog.OnDateSetListener date;
private int pYear;
private int pMonth;
private int pDay;
static final int DATE_DIALOG_ID = 0;
myCalendar = Calendar.getInstance();
pYear = myCalendar.get(Calendar.YEAR);
pMonth = myCalendar.get(Calendar.MONTH);
pDay = myCalendar.get(Calendar.DAY_OF_MONTH);
dobET.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
public Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
date = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
pYear = year;
pMonth = monthOfYear;
pDay = dayOfMonth;
updateLabel();
}
};
DatePickerDialog dialog = new DatePickerDialog(this, date, pYear, pMonth, pDay);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
}
return null;
}
private void updateLabel() {
String myFormat = "MM-dd-yyyy"; // In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
String myFormat2 = "yyyy-MM-dd"; // In which you need put here
SimpleDateFormat sdf2 = new SimpleDateFormat(myFormat2, Locale.US);
dateToSave = sdf2.format(myCalendar.getTime());
saveDOBPrefEditor.putString("DOB", dateToSave);
saveDOBPrefEditor.commit();
dobET.setText(sdf.format(myCalendar.getTime()));
}
您更新'pBlaBla'在'onDateSet()'中,但是你可以在'updateLabel()'中使用'myCalendar'。更新任一個,然後將它們用於'updateLabel()'。 – 2014-09-26 06:15:19
你可以寫代碼我很困惑這個 – Aparana 2014-09-26 06:30:22
@ user3876078試試我的答案,如果它不起作用告訴我我會編輯你的 – Umair 2014-09-26 06:32:03