我正在使用以下内容 日期选择器的示例
http://developer.android.com/guide/tutorials/views/hello-datepicker.html
现在我想在点击日期选择器取消按钮时执行一些功能,但是在datepickerdialog中没有看到取消事件
任何人都可以指导我如何实现这一目标。
任何帮助将不胜感激。
答案 0 :(得分:66)
我是这样做的:
DatePickerDialog dialog = new DatePickerDialog(this,
mDateSetListener,
year, month, day);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
// Do Stuff
}
}
});
答案 1 :(得分:5)
如果要根据用户是否选择日期执行不同的操作,可以使用onDismiss处理程序。请务必设置布尔值(例如“isDataSet”)以指示用户是否选择了日期。这是一个例子:
// Date Picker Dialog
public void showDatePickerDialog(View view) {
int year, month, day;
isDataSet = false; // this is used by the onDismiss handler
// Set initial time period in DatePicker to current month
calendarCurrent = Calendar.getInstance();
month = calendarCurrent.get(Calendar.MONTH);
day = calendarCurrent.get(Calendar.DAY_OF_MONTH);
year = calendarCurrent.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(YourActivity.this, dateSetListener, year, month, day );
datePickerDialog.setOnDismissListener(mOnDismissListener);
datePickerDialog.show();
datePickerDialog_visible=true; //indicate dialog is up
} // [END showDatePickerDialog]
//onDismiss handler
private DialogInterface.OnDismissListener mOnDismissListener =
new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
datePickerDialog_visible=false; //indicate dialog is cancelled/gone
if (isDataSet) { // [IF date was picked
//do something, date now selected
} else {
//do someething else, dialog cancelled or exited
}
}
};
答案 2 :(得分:4)
DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
}
});
dialog.show();
答案 3 :(得分:3)
由于DatePickerDialog
是Dialog
,因此它支持以下内容:
setOnCancelListener(OnCancelListener listener)
setOnDismissListener(OnDismissListener listener)
使用两个侦听器,您可以识别日期选择器对话框的所有解雇。第一个问题是答案,后者是完成问题。
答案 4 :(得分:1)
DatePickerDialog现在公开了一个onCancel侦听器。
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
// Do whatever you want when the date is selected.
editText.setText(String.format("%04d-%02d-%02d", year, month+1, day));
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
}
});
答案 5 :(得分:0)
在这部分代码中:
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
您还必须实施onClick():
public void onClick (DialogInterface dialog, int which) {
if (which == Button.NEGATIVE) {
//Cancel
}
}
的API
答案 6 :(得分:0)
以下代码帮助我解决了“取消”按钮未关闭“日期”选择器窗口的问题。感谢eSilver的解决方案。
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
datePickerPlugin.success(new PluginResult(PluginResult.Status.OK, ""), callBackId);
dateDialog.hide();
}
}
});
答案 7 :(得分:0)
很抱歉弹出古老的问题,但我遇到了类似的问题,并用正确的行为编写了标准DatePickerDialog的简单继承者。看到它in this topic.我希望这会有所帮助。
答案 8 :(得分:0)
public void showDatePicker() {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker = new DatePickerDialog(getActivity(), (view, year1, month1, day1) -> {
Calendar c1 = Calendar.getInstance();
c1.set(year1, month1, day1);
Date date = c1.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
String d = formatter.format(date);
// btnDatePicker.setText(d);
// selectedDate = d;
}, year, month, day) {
@Override
public void onClick(@NonNull DialogInterface dialog, int which) {
super.onClick(dialog, which);
if (which == DialogInterface.BUTTON_POSITIVE) {
} else if (which == DialogInterface.BUTTON_POSITIVE) {
}
}
};
datePicker.getDatePicker().setMinDate(System.currentTimeMillis());
datePicker.show();
}
答案 9 :(得分:0)
我在@PeteH很好的答案中添加了一些其他代码(该代码可对用户按下日期或取消操作做出反应)。
private void showDatePickerDialog() {
final int[] year = new int[1];
final int[] month = new int[1];
final int[] day = new int[1];
isDataSet = false; // this is used by the onDismiss handler
// Set initial time period in DatePicker to current month
Calendar calendarCurrent = Calendar.getInstance();
month[0] = calendarCurrent.get(Calendar.MONTH);
day[0] = calendarCurrent.get(Calendar.DAY_OF_MONTH);
year[0] = calendarCurrent.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(
MyActivity,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int y, int m, int d) {
isDataSet =true;
//user confirms date
year[0] =y;
month[0] = m;
day[0] =d;
//do something with date selected
}
},
year[0],
month[0],
day[0]
);
datePickerDialog.setOnDismissListener(mOnDismissListener);
datePickerDialog.show();
}
private DialogInterface.OnDismissListener mOnDismissListener =new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
if (!isDataSet){
//do something if user cancels
}
};
答案 10 :(得分:0)
简单来说,
如果您正在使用或扩展AppCompatDialogFragment()
只需覆盖
override fun onCancel(dialog: DialogInterface) {
super.onCancel(dialog)
// write your own code
}
就是这样:)
答案 11 :(得分:-3)
试试这个:
if (resultCode == RESULT_CANCELED) {
Toast toast = Toast.makeText(this,"Datepicker Canceled", 10000);
toast.show();
return;
}