我面临的问题是从日期和时间对话框。我已经单独创建了所有这些。在 onCreate 上,我在对话框中为用户设置了当前日期和时间。即例如 24/7/2012 作为今天的日期。当我将 from_date 的对话框更改/单击为日期时,例如:26/7/2012 然后我通过 setText 将 to_date 对话框的文本更改为 26/7/2012。这里出现的问题是当我打开 to_date 对话框时,对话框中显示的日期是 24/7/2012 而不是 26/7/2012。对于日期和时间选择器对话框,如何实现这一点。我附上了下面的代码。提前致谢。
public Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID_FROM:
Log.i("Date Case", "" + from_day);
return new DatePickerDialog(this, dateListenerfrom, from_year,
from_month, from_day);
case DATE_DIALOG_ID_TO:
Log.i("Date Case", "" + to_day);
return new DatePickerDialog(this, dateListenerto, to_year,
to_month, to_day);
case TIME_DIALOG_ID_FROM:
return new TimePickerDialog(this, timeListenerfrom, from_hours,
from_min, false);
case TIME_DIALOG_ID_TO:
return new TimePickerDialog(this, timeListenerto, to_hours, to_min,
false);
}
return null;
}
private DatePickerDialog.OnDateSetListener dateListenerfrom = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int yr, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
from_year = yr;
from_month = monthOfYear;
from_day = dayOfMonth;
// to_year = yr;
// to_month = monthOfYear;
to_day = dayOfMonth;
Log.i("From Day", "" + from_day);
Log.i("To Day", "" + to_day);
Log.i("To Cons", "" + dayOfMonth);
try {
updateDateFrom();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private DatePickerDialog.OnCancelListener mDateFromCancelListener = new DatePickerDialog.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
}
};
private DatePickerDialog.OnDateSetListener dateListenerto = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int yr, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
to_year = yr;
to_month = monthOfYear;
to_day = dayOfMonth;
Log.i("From Day", "" + from_day);
Log.i("To Day", "" + to_day);
Log.i("To Cons", "" + dayOfMonth);
try {
updateDateTo();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private void updateDateFrom() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
try {
date_from = dateFormat.parse(from_day + "/" + from_month + "/"
+ from_year + " " + from_hours + ":" + to_min);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("From LONG " + date_from.getTime());
from_date_builder = new StringBuilder().append(from_day).append('/')
.append(from_month + 1).append('/').append(from_year);
from_date.setText(from_date_builder.toString());
to_date.setText(new StringBuilder().append(from_day).append('/')
.append(from_month + 1).append('/').append(from_year));
}
private void updateDateTo() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
try {
date_to = dateFormat.parse(to_day + "/" + to_month + "/" + to_year
+ " " + to_hours + ":" + to_min);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("To LONG " + date_to.getTime());
if (date_from.getTime() <= date_to.getTime()) {
to_date_builder = new StringBuilder().append(to_day).append('/')
.append(to_month + 1).append('/').append(to_year);
to_date.setText(to_date_builder.toString());
} else {
Toast.makeText(this, "Please set proper date", Toast.LENGTH_SHORT)
.show();
}
}
private TimePickerDialog.OnTimeSetListener timeListenerfrom = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
from_hours = hourOfDay;
from_min = minute;
try {
updateTimeFrom();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private TimePickerDialog.OnTimeSetListener timeListenerto = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
to_hours = hourOfDay;
to_min = minute;
try {
updateTimeTo();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private void updateTimeFrom() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
time_from = dateFormat.parse(from_day + "/" + from_month + "/"
+ from_year + " " + from_hours + ":" + from_min);
from_time_builder = new StringBuilder().append(from_hours).append(':')
.append(from_min);
from_time.setText(from_time_builder.toString());
to_time.setText(new StringBuilder().append(from_hours).append(':')
.append(from_min));
}
private void updateTimeTo() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
time_to = dateFormat.parse(to_day + "/" + to_month + "/" + to_year
+ " " + to_hours + ":" + to_min);
if (time_from.getTime() <= time_to.getTime()
&& date_from.getTime() <= date_to.getTime()) {
to_time_builder = new StringBuilder().append(to_hours).append(':')
.append(to_min);
to_time.setText(to_time_builder.toString());
}
else {
Toast.makeText(this, "Please set proper date", Toast.LENGTH_SHORT)
.show();
}
System.out.println("Thi is in the time to ");
}
这是 onClick {
if (v == from_date) {
showDialog(DATE_DIALOG_ID_FROM);
}
if (v == to_date) {
showDialog(DATE_DIALOG_ID_TO);
}
if (v == from_time) {
showDialog(TIME_DIALOG_ID_FROM);
}
if (v == to_time) {
showDialog(TIME_DIALOG_ID_TO);
}
}