TimePickerDialog Cancel Button
Solution 1:
Just pass an onCancelListener to TimePicker.setOnCancelListener()
edit: After Ron's problems with implementation I decided to actually test myself the code (I answered just looking at the API) and I discovered than even if the code is correct (I suppose you had a typo somewhere as my code compiles ok), when clicking the cancel button it didn't respond as intended...
It appears that when you click cancel
button the Dialog doesn't call the cancel()
method that fires the OnCancelListener
as it would seem the obvious, but the dismiss()
method that fires an OnDismissListener
, pretty weird...
So this code is working fine for me:
TimePickerDialog.OnTimeSetListener mTimeSetListener = new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//time set stuff
}
};
TimePickerDialog myTPDialog = new TimePickerDialog(this,mTimeSetListener,0,0,false);
myTPDialog.setOnDismissListener(new OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
// Cancel code here
}
});
myTPDialog.show();
all credits to this SO answer...
Solution 2:
here is how I did it:
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);
}
}
});
Solution 3:
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();
Solution 4:
My solution is based on @maid450 answer, remark of @Fortega and a fact that OnDismissListener is called (usually in last turn) on any click when a time picker dialog is shown.
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();
Solution 5:
I've just override (kotlin sample code)
override fun onCancel(dialog: DialogInterface?) {
super.onCancel(dialog)
callback?.onCancelClick()
}
method inside my DialogFragment and called onCancelClick of my interface:
interface SetAlarmTimeListener: TimePickerDialog.OnTimeSetListener {
fun onCancelClick()
}
Post a Comment for "TimePickerDialog Cancel Button"