TimePickerDialog 和 TimePicker

发布于 2024-12-03 09:15:37 字数 846 浏览 2 评论 0原文

onCreate(...) {
...
showDialog(TIME_DIALOG_ID);
timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
...
Button btnOpen = (Button) findViewById(R.id.btnSet);
btnOpen.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
        Toast.makeText(getBaseContext(),
                       “Time selected:” +
                       timePicker.getCurrentHour().toString() +
                       “:” + timePicker.getCurrentMinute().toString(),
                       Toast.LENGTH_SHORT).show();
     }
}

onCreateDialog(...) {
    return new TimePickerDialog(...)
}

上面是 Android 书籍中的代码示例。我想知道 timePicker 如何知道当前小时和当前分钟。 timePicker仅在onCreate中定义。当使用 showDialog() 调用 onCreateDialog 时,它返回一个 TimePickerDialog,它似乎根本不知道 timePicker,但 timePicker 能够获取当前的小时和分钟来烘烤它。

onCreate(...) {
...
showDialog(TIME_DIALOG_ID);
timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
...
Button btnOpen = (Button) findViewById(R.id.btnSet);
btnOpen.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
        Toast.makeText(getBaseContext(),
                       “Time selected:” +
                       timePicker.getCurrentHour().toString() +
                       “:” + timePicker.getCurrentMinute().toString(),
                       Toast.LENGTH_SHORT).show();
     }
}

onCreateDialog(...) {
    return new TimePickerDialog(...)
}

Above is a code sample from an Android book. I was wondering how timePicker knows the current hour and current minute. timePicker is only defined in onCreate. When onCreateDialog is called with showDialog(), it returns a TimePickerDialog that in no way seems to know about timePicker, yet timePicker is able to get the current hour and minute to toast it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

‖放下 2024-12-10 09:15:37

TimePickerDialog 包含一个 TimePicker 小部件,它又实现了获取当前时间的代码。示例代码引用 TimePickerDialog 直接假设 TimePickerDialog 将始终包含 TimePicker,id 为:timePicker

但是,您不需要神奇地知道时间选择器。您可以实现 TimePicker.OnTimeChangedListener 回调接口,该接口可以访问通过 onTimeChanged 嵌入的 TimePicker (TimePicker view, int hourOfDay, int 分钟) 方法,如

TimePickerDialog contains a TimePicker widget which in turn implements code for getting the current time. The sample code is referencing the TimePicker in TimePickerDialog directly by assuming that TimePickerDialog will always contain a TimePicker with the id: timePicker.

However, you don't need to magically know the id of the TimePicker. You can implement the TimePicker.OnTimeChangedListener callback interface which has access to the embedded TimePicker via the onTimeChanged (TimePicker view, int hourOfDay, int minute) method as illustrated in this Time Picker tutorial instead.

何其悲哀 2024-12-10 09:15:37

当您调用 showDialog() 时,将在 Activity 的视图层次结构中创建对话框。因此,当下一行执行 findViewById(R.id.timePicker) 时,它会在对话框中找到 TimePicker,大概其 id 为 timePicker >,并将对其的引用保存在 timePicker 中。

When you call showDialog(), the dialog is created in the Activity's View hierarchy. So when the next line does findViewById(R.id.timePicker), it finds the TimePicker inside the dialog, presumably which has the id of timePicker, and saves a reference to it in timePicker.

抠脚大汉 2024-12-10 09:15:37

onCreate 方法中,您只有参考。

当您单击 btnOpen 时,您将调用 getCurrentHour()getCurrentMinute() 来获取当前时间。

On onCreate Method you only have the reference.

when you click on btnOpen you call getCurrentHour() and getCurrentMinute() to get the current time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文