TimePickerDialog 和 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(...)
}
上面是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.当您调用
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 doesfindViewById(R.id.timePicker)
, it finds theTimePicker
inside the dialog, presumably which has the id oftimePicker
, and saves a reference to it intimePicker
.在
onCreate
方法中,您只有参考。当您单击 btnOpen 时,您将调用
getCurrentHour()
和getCurrentMinute()
来获取当前时间。On
onCreate
Method you only have the reference.when you click on btnOpen you call
getCurrentHour()
andgetCurrentMinute()
to get the current time.