Elegant solution to retrieve custom date and time?
I am currently using a date and time picker to retrieve a user-submitted date and time, and then I set a control's text to the date and time selected.
I am using the following code:
new DatePickerDialog(newlog3.this, d, calDT.get(Calendar.YEAR), calDT.get(Calendar.MONTH开发者_如何学C), calDT.get(Calendar.DAY_OF_MONTH)).show();
new TimePickerDialog(newlog3.this, t, calDT.get(Calendar.HOUR_OF_DAY), calDT.get(Calendar.MINUTE), true).show();
optCustom.setText(fmtDT.format(calDT.getTime()));
Now, while the above code block does bring up the date and time widgets and sets the text, the code block is being executed in full before the user can select the date.. ie: It brings up the date box first, then the time box over that, and then updates the text, all without any user interaction. I would like the date widget to wait to execute the time selector until the date selection is done, and i would like the settext to execute only after the time widget is done.
How is this possible? Or is there is a more elegant solution that is escaping me?
Edit: This is the code for DatePickerDialog/TimePickerDialog which is located within the class:
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calDT.set(Calendar.YEAR, year);
calDT.set(Calendar.MONTH, monthOfYear);
calDT.set(Calendar.DAY_OF_MONTH, dayOfMonth);
//updateLabel();
}
};
TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
calDT.set(Calendar.HOUR_OF_DAY, hourOfDay);
calDT.set(Calendar.MINUTE, minute);
//updateLabel();
}
};
Thanks in advance
Listen when DatePickerDialog closes with DatePickerDialog.OnDateSetListener. When onDateSet(...) is called show the next dialog (TimePickerDialog).
Edit: I've pasted code which you asked
public class YourActivity extends Activity {
private final Calendar selectedDate = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
selectedDate.set(Calendar.YEAR, year);
selectedDate.set(Calendar.MONTH, monthOfYear);
selectedDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
new TimePickerDialog(YourActivity.this, timeListener, 0, 0, true).show();
}
};
final TimePickerDialog.OnTimeSetListener timeListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
selectedDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
selectedDate.set(Calendar.MINUTE, minute);
// push flow to the next step
}
};
public void startTimeWizard() {
final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
new DatePickerDialog(this, dateListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startTimeWizard();
}
}
精彩评论