1

TimePicker 对话框检索 onCreate 时间,而不是当前时间,如果活动保持打开它不会改变......我希望对话框始终显示当前时间,而不仅仅是活动开始时。我目前在 onCreate 中使用以下内容:

    final Calendar onscene = Calendar.getInstance();
    buildingfireonsceneHour = onscene.get(Calendar.HOUR_OF_DAY);
    buildingfireonsceneMinute = onscene.get(Calendar.MINUTE);
4

1 回答 1

2

在您打开对话框之前将当前时间传递给 updateTimer() 函数,例如:

    mTimePickerDialog = new TimePickerDialog(this, null, 0, 0, false);

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            Calendar calendar = Calendar.getInstance();
            mTimePickerDialog.updateTime(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
            mTimePickerDialog.show();
        }
    });

** 添加 **

我相信您想像这样覆盖 onPrepareDialog() 函数:

@Override
protected void onPrepareDialog (int id, Dialog dialog, Bundle args) {
    super.onPrepareDialog(id, dialog, args);
    if(id != DATE_DIALOG_ID) {
        final Calendar c = Calendar.getInstance();
        ((TimePickerDialog) dialog).updateTime(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
    }
}
于 2012-05-06T23:05:32.000 回答