我不确定如何在下面的代码中实现onCancel / onDismiss侦听器。有人可以帮忙。
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
time = ""+mHour+":"+mMinute;
notifications();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
}
return null;
}
编辑:工作代码如下。点击设置就可以了。除了复选框之外的任何其他内容。
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
TimePickerDialog myTime = new TimePickerDialog(this,mTimeSetListener, mHour, mMinute, false);
myTime.setOnCancelListener(new OnCancelListener(){
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
public void onCancel(DialogInterface dialog) {
Log.d("IN HERE","HERE2");
cbChecked = false;
notValue = 0;
checkBox.setChecked(false);
checkBox.setText("Use reminders");
}
});
return myTime;
}
return null;
}
答案 0 :(得分:2)
不要像TimePickerDialog
方法那样直接返回onCreateDialog
,而应该实例化它并添加onCancel
侦听器。
...
case TIME_DIALOG_ID:
TimePickerDialog myTime = new TimePickerDialog(this,yourListener, h, m, false);
myTime(new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface arg0) {
//your stuff
}
);//For cancel button
return myTime; //Return the dialog
break;
...