Android 如何检测日期选择器对话框的取消点击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2928902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I detect a cancel click of the datepicker dialog?
提问by UMAR
i am using following example of date picker
我正在使用以下日期选择器示例
http://developer.android.com/guide/tutorials/views/hello-datepicker.html
http://developer.android.com/guide/tutorials/views/hello-datepicker.html
now i want to perform some functionality on clicking date picker cancel button but dont see cancel event inside datepickerdialog
现在我想在单击日期选择器取消按钮时执行一些功能,但在 datepickerdialog 中看不到取消事件
can any one guide me how to achieve this.
任何人都可以指导我如何实现这一目标。
any help would be appreciated.
任何帮助,将不胜感激。
回答by esilver
Here's how I did it:
这是我如何做到的:
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
}
}
});
回答by PeteH
If you want to perform a different action depending on whether the user selected a date or not you can use an onDismiss handler. Be sure to set a Boolean (e.g., "isDataSet") to indicate whether the user selected a date or not. Here's an example:
如果您想根据用户是否选择日期执行不同的操作,您可以使用 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
}
}
};
回答by Karan Datwani
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();
回答by Eran Boudjnah
Since DatePickerDialog
is a Dialog
, it supports the following:
由于DatePickerDialog
是 a Dialog
,它支持以下内容:
setOnCancelListener(OnCancelListener listener)
setOnDismissListener(OnDismissListener listener)
Using both listeners, you can identify all dismissals of the date picker dialog. The first would answer the question, the latter is for completion.
使用这两个侦听器,您可以识别日期选择器对话框的所有关闭。第一个会回答问题,后者是为了完成。
回答by Harsh
DatePickerDialog now exposes a onCancel listener.
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) {
}
});
回答by seekingStillness
I added some additional code to @PeteH great answer (which provides for reacting to user pressing date or cancelling).
我向@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
}
};
回答by Mina Samir
Simply,
if you are using or extending AppCompatDialogFragment()
just override the
简单地说,如果您正在使用或扩展AppCompatDialogFragment()
只是覆盖
override fun onCancel(dialog: DialogInterface) {
super.onCancel(dialog)
// write your own code
}
That is it :)
这就对了 :)
回答by aanshu
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();
}
回答by aanshu
In this part of the code:
在这部分代码中:
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();
}
};
you have to implement also onClick():
你还必须实现onClick():
public void onClick (DialogInterface dialog, int which) {
if (which == Button.NEGATIVE) {
//Cancel
}
}
API for Button.NEGATIVE
回答by Vijay
The following code helped me solve an issue with the Cancel button not closing out the Date picker window. Thanks to eSilver for the solution.
以下代码帮助我解决了取消按钮未关闭日期选择器窗口的问题。感谢 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();
}
}
});