我有一个活动 - TimePickerActivity - 它创建一个TimePickerDialog。我有一个onTimeSetListener响应Set按钮,在它结束时调用finish()并返回调用TimePickerActivity的活动。我希望它完成并返回如果单击取消按钮但无法找到任何方法来执行此操作。我用谷歌搜索并尝试了几个建议,但似乎没有一个工作。有什么简单的方法吗?
答案 0 :(得分:9)
只需将onCancelListener传递给TimePicker.setOnCancelListener()
即可编辑在Ron遇到实施问题后,我决定亲自测试自己的代码(我回答的只是查看API),我发现即使代码是正确的(我想你有一个在我的代码编译好的某个地方错字),当点击取消按钮时它没有按预期响应...
当您点击cancel
按钮时,对话框似乎没有调用触发cancel()
的{{1}}方法,因为它看起来很明显,但是OnCancelListener
触发dismiss()
的方法,很奇怪......
所以这段代码对我来说很好:
OnDismissListener
所有信用额this所以答案......
答案 1 :(得分:4)
这是我的表现:
TimePickerDialog tp = new TimePickerDialog(this, mTimeSetListener, 0, 0, false);
tp.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
if (which == DialogInterface.BUTTON_NEGATIVE)
{
tbTimer.setChecked(false);
}
}
});
答案 2 :(得分:0)
我的解决方案基于@maid450答案,@ Fortega的评论以及在显示时间选择器对话框时在任何点击上调用OnDismissListener(通常在最后一轮)的事实。
private int timeSetStatus; // A clicked button result.
...
timeSetStatus = 0;
final Calendar calendar = Calendar.getInstance();
final TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
timeSetStatus = 1;
// Other actions.
}
}, calendar.get(Calendar.HOUR_OF_DAY), 0, true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
timeSetStatus = 2;
// Actions on clicks outside the dialog.
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (timeSetStatus == 0) {
// Actions on Cancel button click.
}
}
});
dialog.show();
答案 3 :(得分:0)
我只是覆盖(kotlin示例代码)
override fun onCancel(dialog: DialogInterface?) {
super.onCancel(dialog)
callback?.onCancelClick()
}
我的DialogFragment中的方法,并调用我的界面的onCancelClick:
interface SetAlarmTimeListener: TimePickerDialog.OnTimeSetListener {
fun onCancelClick()
}
答案 4 :(得分:0)
TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
//ok button clicked
}
};
Calendar c = Calendar.getInstance();
int now_hour = c.get(Calendar.HOUR_OF_DAY);
int now_minutes = c.get(Calendar.MINUTE);
final TimePickerDialog timePickerDialog = new TimePickerDialog(this, timePickerListener, now_hour, now_minutes + 1, false);
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//cancel button clicked
}
});
timePickerDialog.show();
答案 5 :(得分:0)
最后一个正在运行的解决方案......
在类中声明一个布尔变量,如下所示:
private boolean isTimeSelected;
然后在按钮上单击调用以下方法:
public void onButtonClicked() {
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP || isTimeSelected) {
lastSelectedHour = selectedHour;
lastSelectedMinute = selectedMinute;
// perform OK button tasks
}
}
}, lastSelectedHour, lastSelectedMinute, true);
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
// perform CANCEL button tasks
}
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
timePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
isTimeSelected = true;
}
}
});
timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
isTimeSelected = false;
}
}
});
timePickerDialog.setCancelable(false);
timePickerDialog.setTitle("Select Time");
}
timePickerDialog.show();
}