问题描述
我用下面的code显示 TimePickerDialog
:
I'm using the following code to display a TimePickerDialog
:
TimePickerDialog dialog = new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
System.out.println("onTimeSet");
}
}, 20, 15, true);
dialog.setTitle("Test");
dialog.setButton(TimePickerDialog.BUTTON_POSITIVE, "Done", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("Done");
}
});
dialog.setButton(TimePickerDialog.BUTTON_NEGATIVE, "Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("Cancel");
}
});
dialog.show();
在运行Android 2.3.7的设备,则 onTimeSet
方法不会被调用(在的onClick
方法做)。在运行4.2.2设备的 onTimeSet
不会被调用像它应该。
On a device running Android 2.3.7, the onTimeSet
method doesn't get called (the onClick
methods do). On a device running 4.2.2 the onTimeSet
does get called like it should.
这是怎么回事?
推荐答案
TimePickerDialog的Android 4.1.1后发生了变化,有一个关于取消双方TimePickerDialog和DatePickerDialog的错误。请先阅读here.缺省情况下不需要设置正和负按钮。 TimePickerDialog和DatePickerDialog处理这些对你。所以,如果取消这个问题不是重要的,你删除的正面和负面的按键设置。如果您删除那些在这两个版本,如果用户点击OK按钮的onTimeSet方法将被调用。
TimePickerDialog has changed after Android 4.1.1 and there is a bug about cancellation of the both TimePickerDialog and DatePickerDialog. Please read first here. By default you do not need to set positive and negative button. TimePickerDialog and DatePickerDialog handles these for you. So if this cancellation issue is not important for you delete setting of positive and negative button. If you delete those in both version if user clicks OK button your onTimeSet method will be called.
不过,我建议,直到该错误将是固定的使用习惯做AlertDialog与TimePicker部件;
But I recommend until that bug will be fixed use custom made AlertDialog with TimePicker widget;
final TimePicker timePicker = new TimePicker(this);
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(20);
timePicker.setCurrentMinute(15);
new AlertDialog.Builder(this)
.setTitle("Test")
.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Picker", timePicker.getCurrentHour() + ":"
+ timePicker.getCurrentMinute());
}
})
.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Log.d("Picker", "Cancelled!");
}
}).setView(timePicker).show();
这篇关于TimePickerDialog.onTimeSetListener不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!