TimePicker Dialog在创建时检索,而不是当前时间,如果活动保持打开状态,它将不会更改

时间:2012-05-06 21:56:14

标签: android timepicker

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

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

1 个答案:

答案 0 :(得分: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));
    }
}