我目前正在使用日期和时间选择器来检索用户提交的日期和时间,然后我将控件的文本设置为所选的日期和时间。
我使用以下代码:
new DatePickerDialog(newlog3.this, d, calDT.get(Calendar.YEAR), calDT.get(Calendar.MONTH), 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()));
现在,虽然上面的代码块确实显示了日期和时间小部件并设置了文本,但是在用户可以选择日期之前,代码块正在完整执行..即:它首先显示日期框,然后是时间框,然后更新文本,所有这些都没有任何用户交互。我希望日期窗口小部件等待执行时间选择器,直到完成日期选择,并且我希望settext仅在完成时间窗口小部件后执行。
这怎么可能?或者是否有更优雅的解决方案可以逃避我?
编辑:这是DatePickerDialog / TimePickerDialog的代码,它位于类中:
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();
}
};
提前致谢
答案 0 :(得分:2)
在 DatePickerDialog 以DatePickerDialog.OnDateSetListener结束时收听。调用onDateSet(...)时显示下一个对话框(TimePickerDialog)。
编辑:我已粘贴您提出的代码
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();
}
}