我正在使用一个按钮,用这个方法显示团队的小时和分钟
private void obtenerFechaActual() {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
}
btnSelectTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Show the TimePickerDialog
obtenerFechaActual();
showDialog(TIME_DIALOG_ID);
}
});
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int min) {
hour = hourOfDay;
minute = min;
btnSelectTime.setText("Time selected :"+hour+"-"+minute);
}
};
确实很好,但每次按下按钮,它都会保留按钮第一次点击的分钟和日期。我们的想法是,每次单击按钮,它都会显示当前时间和日期。
答案 0 :(得分:0)
添加此方法:
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case TIME_DIALOG_ID:
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
break;
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}