--- 主持人请注意: 今天(7月15日),我注意到有人已经遇到过这个问题here。但是我不确定将这个作为副本关闭是否合适,因为我认为我提供了一个更好的解释。我不确定是否应该编辑其他问题并在此处粘贴此内容,但我不太愿意过多地改变别人的问题。 ---
我这里有一些怪异的。
我不认为问题取决于您构建的SDK。设备操作系统版本非常重要。
DatePickerDialog
(?),现在只提供完成按钮。以前的版本包含取消按钮,这可能会影响用户体验(不一致,以前Android版本的肌肉记忆)。
复制: 创建基本项目。把它放在onCreate
:
DatePickerDialog picker = new DatePickerDialog(
this,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker v, int y, int m, int d) {
Log.d("Picker", "Set!");
}
},
2012, 6, 15);
picker.show();
预期: 取消按钮显示在对话框中。
当前: 取消按钮不会出现。
屏幕截图: 4.0.3(确定)和4.1.1(可能是错误的?)。
Dialog会调用它应该调用的侦听器,然后总是调用OnDateSetListener
侦听器。取消仍然调用set方法,并设置它调用方法两次。
复制: 使用#1代码,但在下面添加代码(您会看到这解决了#1,但只能直观/用户界面):
picker.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Picker", "Cancel!");
}
});
预期:
电流:的
显示行为的日志行:
07-15 12:00:13.415: D/Picker(21000): Set!
07-15 12:00:24.860: D/Picker(21000): Cancel!
07-15 12:00:24.876: D/Picker(21000): Set!
07-15 12:00:33.696: D/Picker(21000): Set!
07-15 12:00:33.719: D/Picker(21000): Set!
DatePickerFragment
附近并不重要。我为你简化了问题,但我已经测试了它。答案 0 :(得分:115)
注意: Fixed as of Lollipop,source here。自动class for use in clients(与所有Android版本兼容)也已更新。
OnDateSetListener
(或更改课程以满足您的需求)。使用此代码触发对话框(在此示例中,我在Fragment
中使用它):
Bundle b = new Bundle();
b.putInt(DatePickerDialogFragment.YEAR, 2012);
b.putInt(DatePickerDialogFragment.MONTH, 6);
b.putInt(DatePickerDialogFragment.DATE, 17);
DialogFragment picker = new DatePickerDialogFragment();
picker.setArguments(b);
picker.show(getActivity().getSupportFragmentManager(), "frag_date_picker");
这就是全部所需!我仍然保持我的答案“接受”的原因是因为我仍然更喜欢我的解决方案,因为它在客户端代码中的占用空间非常小,它解决了根本问题(在框架类中调用侦听器),在配置更改中工作正常,并将代码逻辑路由到以前未受此错误困扰的Android版本中的默认实现(请参阅类源代码)。
好吧,看起来确实是一个错误而其他人已经填满了它。 Issue 34833
我发现问题可能在DatePickerDialog.java
。它的内容如下:
private void tryNotifyDateSet() {
if (mCallBack != null) {
mDatePicker.clearFocus();
mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
}
}
@Override
protected void onStop() {
tryNotifyDateSet();
super.onStop();
}
我猜它可能是:
@Override
protected void onStop() {
// instead of the full tryNotifyDateSet() call:
if (mCallBack != null) mDatePicker.clearFocus();
super.onStop();
}
现在,如果有人能告诉我如何向Android提出补丁/错误报告,我会很高兴。与此同时,我在问题中提出了一个可能的修复(简单)作为DatePickerDialog.java
的附加版本。
在构造函数中将侦听器设置为null
,稍后在上创建自己的BUTTON_POSITIVE
按钮。就是这样,详情如下。
问题发生的原因是DatePickerDialog.java
,正如您在源代码中看到的那样,调用一个全局变量(mCallBack
)来存储构造函数中传递的侦听器:
/**
* @param context The context the dialog is to run in.
* @param callBack How the parent is notified that the date is set.
* @param year The initial year of the dialog.
* @param monthOfYear The initial month of the dialog.
* @param dayOfMonth The initial day of the dialog.
*/
public DatePickerDialog(Context context,
OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth) {
this(context, 0, callBack, year, monthOfYear, dayOfMonth);
}
/**
* @param context The context the dialog is to run in.
* @param theme the theme to apply to this dialog
* @param callBack How the parent is notified that the date is set.
* @param year The initial year of the dialog.
* @param monthOfYear The initial month of the dialog.
* @param dayOfMonth The initial day of the dialog.
*/
public DatePickerDialog(Context context,
int theme,
OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth) {
super(context, theme);
mCallBack = callBack;
// ... rest of the constructor.
}
所以,诀窍是提供一个null
监听器作为监听器存储,然后滚动你自己的一组按钮(下面是来自#1的原始代码,更新):
DatePickerDialog picker = new DatePickerDialog(
this,
null, // instead of a listener
2012, 6, 15);
picker.setCancelable(true);
picker.setCanceledOnTouchOutside(true);
picker.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Picker", "Correct behavior!");
}
});
picker.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Picker", "Cancel!");
}
});
picker.show();
现在它会起作用,因为我上面发布了可能的更正。
由于DatePickerDialog.java
每当null
mCallback
clearFocus()
检查class YourActivity extends SherlockFragmentActivity implements OnDateSetListener
// ...
Bundle b = new Bundle();
b.putInt(DatePickerDialogFragment.YEAR, 2012);
b.putInt(DatePickerDialogFragment.MONTH, 6);
b.putInt(DatePickerDialogFragment.DATE, 17);
DialogFragment picker = new DatePickerDialogFragment();
picker.setArguments(b);
picker.show(getActivity().getSupportFragmentManager(), "fragment_date_picker");
时就会检查{{1}} ---当然无法检查Honeycomb,它赢了'触发异常。考虑到Lollipop修复了这个问题,我不打算研究它:只使用默认实现(在我提供的类中提到)。
起初我害怕不打电话给{{1}},但我在这里测试过,日志线很干净。所以我提出的这条线毕竟可能不是必要的,但我不知道。
正如我在下面的评论中指出的那样,这是一个概念,你可以since the days of API 3/1.5 it seems。我使用的方式是,默认系统实现用于不受bug影响的版本。
我采取了一些适合我需要的假设(按钮名称等),因为我希望将客户端类中的样板代码减少到最少。完整用法示例:
{{1}}
答案 1 :(得分:16)
我要在David Cesarino发布的解决方案中添加我自己的riff,以防你没有使用Fragments,并希望在所有版本(2.1至4.1)中轻松修复它:
public class FixedDatePickerDialog extends DatePickerDialog {
//I use a Calendar object to initialize it, but you can revert to Y,M,D easily
public FixedDatePickerDialog(Calendar dateToShow, Context context, OnDateSetListener callBack) {
super(context, null, dateToShow.get(YEAR), dateToShow.get(MONTH), dateToShow.get(DAY_OF_MONTH));
initializePicker(callBack);
}
public FixedDatePickerDialog(Calendar dateToShow, Context context, int theme,
OnDateSetListener callBack) {
super(context, theme, null, dateToShow.get(YEAR), dateToShow.get(MONTH), dateToShow.get(DAY_OF_MONTH));
initializePicker(callBack);
}
private void initializePicker(final OnDateSetListener callback) {
try {
//If you're only using Honeycomb+ then you can just call getDatePicker() instead of using reflection
Field pickerField = DatePickerDialog.class.getDeclaredField("mDatePicker");
pickerField.setAccessible(true);
final DatePicker picker = (DatePicker) pickerField.get(this);
this.setCancelable(true);
this.setButton(DialogInterface.BUTTON_NEGATIVE, getContext().getText(android.R.string.cancel), (OnClickListener) null);
this.setButton(DialogInterface.BUTTON_POSITIVE, getContext().getText(android.R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
picker.clearFocus(); //Focus must be cleared so the value change listener is called
callback.onDateSet(picker, picker.getYear(), picker.getMonth(), picker.getDayOfMonth());
}
});
} catch (Exception e) { /* Reflection probably failed*/ }
}
}
答案 2 :(得分:8)
在修复bug之前,我建议不要使用DatePickerDialog或TimePickerDialog。使用自定义AlertDialog和TimePicker / DatePicker小部件;
使用;
更改TimePickerDialog final TimePicker timePicker = new TimePicker(this);
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(20);
timePicker.setCurrentMinute(15);
new AlertDialog.Builder(this)
.setTitle("Test")
.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Picker", timePicker.getCurrentHour() + ":"
+ timePicker.getCurrentMinute());
}
})
.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Log.d("Picker", "Cancelled!");
}
}).setView(timePicker).show();
使用;
更改DatePickerDialog final DatePicker datePicker = new DatePicker(this);
datePicker.init(2012, 10, 5, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
datePicker.setCalendarViewShown(false);
}
new AlertDialog.Builder(this)
.setTitle("Test")
.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Picker", datePicker.getYear() + " "
+ (datePicker.getMonth() + 1) + " "
+ datePicker.getDayOfMonth());
}
})
.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Log.d("Picker", "Cancelled!");
}
}).setView(datePicker).show();
答案 3 :(得分:4)
TimePicker基于 David Cesarino的解决方案,“TL; DR:1-2-3全球解决方案的简单步骤”
TimePickerDialog不提供DatePickerDialog.getDatePicker等功能。 因此,必须提供 OnTimeSetListener 侦听器。 为了保持与DatePicker解决方案解决方案的相似性,我保持了旧的mListener概念。如果需要,您可以更改它。
Calling and Listener与原始解决方案相同。 只需加入
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
扩展父类,
... implements OnDateSetListener, OnTimeSetListener
实施
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
...
}
示例调用
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
Bundle b = new Bundle();
b.putInt(TimePickerDialogFragment.HOUR, hour);
b.putInt(TimePickerDialogFragment.MINUTE, minute);
DialogFragment picker = new TimePickerDialogFragment();
picker.setArguments(b);
picker.show(getSupportFragmentManager(), "frag_time_picker");
(已更新以处理取消)
public class TimePickerDialogFragment extends DialogFragment {
public static final String HOUR = "Hour";
public static final String MINUTE = "Minute";
private boolean isCancelled = false; //Added to handle cancel
private TimePickerDialog.OnTimeSetListener mListener;
//Added to handle parent listener
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (!isCancelled)
{
mListener.onTimeSet(view,hourOfDay,minute);
}
}
};
//
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mListener = (TimePickerDialog.OnTimeSetListener) activity;
}
@Override
public void onDetach() {
this.mListener = null;
super.onDetach();
}
@TargetApi(11)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle b = getArguments();
int h = b.getInt(HOUR);
int m = b.getInt(MINUTE);
final TimePickerDialog picker = new TimePickerDialog(getActivity(), getConstructorListener(), h, m,DateFormat.is24HourFormat(getActivity()));
//final TimePicker timePicker = new TimePicker(getBaseContext());
if (hasJellyBeanAndAbove()) {
picker.setButton(DialogInterface.BUTTON_POSITIVE,
getActivity().getString(android.R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
isCancelled = false; //Cancel flag, used in mTimeSetListener
}
});
picker.setButton(DialogInterface.BUTTON_NEGATIVE,
getActivity().getString(android.R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
isCancelled = true; //Cancel flag, used in mTimeSetListener
}
});
}
return picker;
}
private boolean hasJellyBeanAndAbove() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
}
private TimePickerDialog.OnTimeSetListener getConstructorListener() {
return hasJellyBeanAndAbove() ? mTimeSetListener : mListener; //instead of null, mTimeSetListener is returned.
}
}
答案 4 :(得分:3)
根据Ankur Chaudhary关于类似TimePickerDialog
问题的精彩answer,如果我们在onDateSet
内查看给定视图isShown()
,它将会以最小的努力解决整个问题,无需扩展选择器或检查代码周围的一些可怕的标志,甚至检查操作系统版本,只需执行以下操作:
public void onDateSet(DatePicker view, int year, int month, int day) {
if (view.isShown()) {
// read the date here :)
}
}
当然,根据Ankur的回答onTimeSet
也可以这样做
答案 5 :(得分:3)
我的简单解决方案。如果你想让它再次发射,只需运行“resetFired”(比如再次打开对话框)。
private class FixedDatePickerDialogListener implements DatePickerDialog.OnDateSetListener{
private boolean fired;
public void resetFired(){
fired = false;
}
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (fired) {
Log.i("DatePicker", "Double fire occurred.");
return;//ignore and return.
}
//put your code here to handle onDateSet
fired = true;//first time fired
}
}
答案 6 :(得分:3)
如果有人想要快速解决方法,这是我使用的代码:
public void showCustomDatePicker () {
final DatePicker mDatePicker = (DatePicker) getLayoutInflater().
inflate(R.layout.date_picker_view, null);
//Set an initial date for the picker
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);
//Set the date now
mDatePicker.updateDate(year, month, day);
//create the dialog
AlertDialog.Builder mBuilder = new Builder(this);
//set the title
mBuilder.setTitle(getString(R.string.date_picker_title))
//set our date picker
.setView(mDatePicker)
//set the buttons
.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//whatever method you choose to handle the date changes
//the important thing to know is how to retrieve the data from the picker
handleOnDateSet(mDatePicker.getYear(),
mDatePicker.getMonth(),
mDatePicker.getDayOfMonth());
}
})
.setNegativeButton(android.R.string.cancel, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
//create the dialog and show it.
.create().show();
}
其中layout.date_picker_view是一个简单的布局资源,其中DatePicker是唯一的元素:
<!xml version="1.0" encoding="utf-8">
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/date_picker"
android:layout_width="fill_parent"
android:spinnersShown="true"
android:calendarViewShown="false"
android:layout_height="fill_parent"/>
如果你感兴趣的话,这是full tutorial。
答案 7 :(得分:2)
我管理这种情况的方法是使用标志并覆盖onCancel和onDismiss方法。
仅当用户触摸对话框或后退按钮外时才会调用onCancel。 onDismiss总是被称为
在onCancel方法中设置标志有助于在onDismiss方法中过滤用户的意图:取消操作或完成操作。下面是一些显示这个想法的代码。
public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private boolean cancelDialog = false;
private int year;
private int month;
private int day;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, year, month, day);
return dpd;
}
public void setDatePickerDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
cancelDialog = true;
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (!cancelDialog) {
#put the code you want to execute if the user clicks the done button
}
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setDatePickerDate(year, monthOfYear, dayOfMonth);
}
}
答案 8 :(得分:1)
如果您的应用程序不使用操作栏,则有一个非常简单的解决方法。顺便提一下,有些应用程序依赖于此功能,因为取消日期选择器具有特殊含义(例如,它将日期字段清除为空字符串,对于某些应用程序而言,这是一种有效且有意义的输入类型并且使用布尔标志来防止在OK上设置两次日期对于这种情况不会有帮助。
重新。实际修复,您不必创建新按钮或您自己的对话框。关键在于兼容Android的旧版本,有缺陷的版本(4. )以及任何未来版本,当然,后者是不可能确定的。 请注意,在Android 2。中,android.app.Dialog的onStop()根本不执行任何操作,而在4. *中它执行mActionBar.setShowHideAnimationEnabled(false),这仅在您的应用具有操作栏时才有用。 DatePickerDialog中的onStop()继承自Dialog,仅提供mDatePicker.clearFocus()(截至Android源4.3的最新修补程序),这似乎并不重要。
因此,在许多情况下,使用不执行任何操作的方法替换onStop()应该可以修复您的应用并确保在可预见的将来它仍然保留。因此,只需使用您自己的类扩展DatePickerDialog类,并覆盖onStop()whit一个虚拟方法。您还必须根据您的要求提供一个或两个构造函数。 还要注意,不应该试图通过例如过度修改此修复。尝试直接对活动栏执行某些操作,因为这会限制您与最新版Android的兼容性。还要注意,能够为DatePicker的onStop()调用super会很好,因为bug只存在于DatePickerDialog本身的onStop()中,而不是在DatePickerDialog的超类中。但是,这需要你从你的自定义类调用super.super.onStop(),Java不会让你这样做,因为它违背了封装原则:) 下面是我用来修改DatePickerDialog的小班。 我希望这个评论对某些人有用。 Wojtek Jarosz
public class myDatePickerDialog extends DatePickerDialog {
public myDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
}
@Override
protected void onStop() {
// Replacing tryNotifyDateSet() with nothing - this is a workaround for Android bug https://android-review.googlesource.com/#/c/61270/A
// Would also like to clear focus, but we cannot get at the private members, so we do nothing. It seems to do no harm...
// mDatePicker.clearFocus();
// Now we would like to call super on onStop(), but actually what we would mean is super.super, because
// it is super.onStop() that we are trying NOT to run, because it is buggy. However, doing such a thing
// in Java is not allowed, as it goes against the philosophy of encapsulation (the Creators never thought
// that we might have to patch parent classes from the bottom up :)
// However, we do not lose much by doing nothing at all, because in Android 2.* onStop() in androd.app.Dialog //actually
// does nothing and in 4.* it does:
// if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
// which is not essential for us here because we use no action bar... QED
// So we do nothing and we intend to keep this workaround forever because of users with older devices, who might
// run Android 4.1 - 4.3 for some time to come, even if the bug is fixed in later versions of Android.
}
}
答案 9 :(得分:0)
您可以覆盖onCancel()并使用setOnDismissListener()来检测负面用户操作。使用DatePickerDialog.BUTTON_POSITIVE,您知道用户想要设置新日期。
DatePickerDialog mDPD = new DatePickerDialog(
getActivity(), mOnDateSetListener, mYear, mMonth, mDay);
mDPD.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// do something onCancek
setDate = false;
}
});
mDPD.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface arg0) {
// do something onDismiss
setDate = false;
}
});
mDPD.setButton(DatePickerDialog.BUTTON_POSITIVE, "Finish", new DatePickerDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// user set new date
setDate = true;
}
});
然后检查setDate:
public void onDateSet(DatePicker view, int year, int month, int day) {
if(setDate){
//do something with new date
}
}
答案 10 :(得分:0)
一个简单的解决方案是使用布尔值跳过第二次运行
boolean isShow = false; // define global variable
// when showing time picker
TimePickerDialog timeDlg = new TimePickerDialog( this, new OnTimeSetListener()
{
@Override
public void onTimeSet( TimePicker view, int hourOfDay, int minute )
{
if ( isShow )
{
isShow = false;
// your code
}
}
}, 8, 30, false );
timeDlg.setButton( TimePickerDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick( DialogInterface dialog, int which )
{
isShow = false;
}
} );
timeDlg.setButton( TimePickerDialog.BUTTON_POSITIVE, "Set", new DialogInterface.OnClickListener()
{
@Override
public void onClick( DialogInterface dialog, int which )
{
isShow = true;
}
} );
timeDlg.show();
答案 11 :(得分:0)
这是我在取消按钮上的DatePickerDialog的解决方法类,以及通过后退按钮放弃它。以DatePickerDialog的方式复制和使用(因为监听器是有状态的,我们必须在使用时创建新实例,否则需要更多代码才能使其工作)
使用:
new FixedDatePickerDialog(this,
new FixedOnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
if (isOkSelected()) {
// when DONE button is clicked
}
}
}, year, month, day).show();
类别:
public class FixedDatePickerDialog extends DatePickerDialog {
private final FixedOnDateSetListener fixedCallback;
public FixedDatePickerDialog(Context context,
FixedOnDateSetListener callBack, int year, int monthOfYear,
int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
fixedCallback = callBack;
this.setButton(DialogInterface.BUTTON_NEGATIVE,
context.getString(R.string.cancel), this);
this.setButton(DialogInterface.BUTTON_POSITIVE,
context.getString(R.string.done), this);
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == BUTTON_POSITIVE) {
fixedCallback.setOkSelected(true);
} else {
fixedCallback.setOkSelected(false);
}
super.onClick(dialog, which);
}
public abstract static class FixedOnDateSetListener implements
OnDateSetListener {
private boolean okSelected = false;
@Override
abstract public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth);
public void setOkSelected(boolean okSelected) {
this.okSelected = okSelected;
}
public boolean isOkSelected() {
return okSelected;
}
}
}
答案 12 :(得分:0)
我正在使用拣选员,时间选择器和号码拣货员。每当用户选择一个数字时,数字选择器会调用onValueChanged,在选择器被解除之前,所以我已经有了这样的结构,只有当选择器被解除时才对值进行处理:
public int interimValue;
public int finalValue;
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
this.interimValue = newVal;
}
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
this.finalValue = this.interimValue;
}
我将其扩展为为我的按钮设置自定义onClickListeners,并使用参数查看单击了哪个按钮。现在我可以在设置最终值之前检查点击了哪个按钮:
public int interimValue;
public int finalValue;
public boolean saveButtonClicked;
public void setup() {
picker.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.BUTTON_SAVE), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
picker.onClick(dialog, which); // added for Android 5.0
onButtonClicked(true);
}
});
picker.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.BUTTON_CANCEL), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
picker.onClick(dialog, which); // added for Android 5.0
onButtonClicked(false);
}
});
}
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
this.interimValue = newVal;
}
public void onButtonClicked(boolean save) {
this.saveButtonClicked = save;
}
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (this.saveButtonClicked) {
// save
this.finalValue = this.interimValue;
} else {
// cancel
}
}
然后我扩展了它以使用日期和时间选择器的日期和时间类型以及数字选择器的int类型。
我发布了这个,因为我认为它比上面的一些解决方案更简单,但现在我已经包含了所有代码,我想这并不简单!但它很适合我已经拥有的结构。
Lollipop更新:显然所有Android 4.1-4.4设备都没有发生此错误,因为我收到了一些日期和时间选择器未调用onDateSet和onTimeSet的用户的报告回调。该错误已在Android 5.0中正式修复。我的方法只适用于存在错误的设备,因为我的自定义按钮没有调用对话框的onClick处理程序,这是在错误不存在时调用onDateSet和onTimeSet的唯一地方。我更新了上面的代码来调用对话框的onClick,所以现在无论错误是否存在它都能正常工作。
答案 13 :(得分:0)
请尝试以下概念。
DatePickerDialog picker = new DatePickerDialog(
this,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker v, int y, int m, int d) {
Log.d("Picker", "Set!");
}
},
2012, 6, 15);
picker.show();
onDateSet()方法调用两次(如果你正在检查emulator.it调用两次。如果使用真实设备那么它将单次正确调用。如果你使用的是模拟器,那么使用counter.if你在真正的设备工作然后忽略计数器变量。对于真正的设备它为我工作。)
当用户点击DatePickerDialog中的按钮时。为此你应该保持一个计数器值,当mothod第一次调用时没有任何操作,当方法调用第二次时执行操作。
请参阅以下编码片段
static int counter=0; //Counter will be declared globally.
DatePickerDialog picker = new DatePickerDialog(
this,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker v, int y, int m, int d) {
counter++;
if(counter==1) return;
counter=0;
//Do the operations here
}
},
2012, 6, 15);
picker.show();
为了取消datepicker dilalog,它为我工作。对于模拟器它没有工作
DialogInterface.OnClickListener dialogOnClickListener=new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(which==Dialog.BUTTON_NEGATIVE)
{
Log.i(tagName, "dialog negative button clicked");
dialog.dismiss();
}
}
};
mDatePickerDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", dialogOnClickListener);
它为我工作了一个真正的设备。但是对于模拟器它不能正常工作。我认为它是一个android模拟器bug。
答案 14 :(得分:0)
我喜欢David Cesarino上面的答案,但是想要一些替代破损对话框的东西,并且可以在任何可能缺少的对话框上工作取消/有不正确的取消行为。以下是DatePickerDialog / TimePickerDialog的派生类,它们应该作为替换中的drop。这些不是自定义视图。它使用系统对话框,但只是更改取消/返回按钮行为以按预期工作。
这应该适用于API级别3及更高级别。所以,基本上任何版本的Android(我专门在软糖和棒棒糖上测试它)。
DatePickerDialog:
package snappy_company_name_here;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.DatePicker;
/**
* This is a modified version of DatePickerDialog that correctly handles cancellation behavior since it's broken on jellybean and
* kitkat date pickers.
*
* Here is the bug: http://code.google.com/p/android/issues/detail?id=34833
* Here is an SO post with a bunch of details: http://stackoverflow.com/questions/11444238/jelly-bean-datepickerdialog-is-there-a-way-to-cancel
*
* @author stuckj, created on 5/5/15.
*/
public class DatePickerDialog extends android.app.DatePickerDialog implements DialogInterface.OnClickListener
{
final CallbackHelper callbackHelper;
// NOTE: Must be static since we're using it in a super constructor call. Which is annoying, but necessary
private static class CallbackHelper implements OnDateSetListener
{
private final OnDateSetListener callBack;
private boolean dialogButtonPressHandled = false; // To prevent setting the date when the dialog is dismissed...
// NOTE: Must be static since we're using it in a super constructor call. Which is annoying, but necessary
public CallbackHelper(final OnDateSetListener callBack)
{
this.callBack = callBack;
}
@Override
public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth)
{
if (!dialogButtonPressHandled && (callBack != null))
{
callBack.onDateSet(view, year, monthOfYear, dayOfMonth);
}
}
}
/**
* Sets the positive and negative buttons to use the dialog callbacks we define.
*/
private void setButtons(final Context context)
{
setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(android.R.string.cancel), this);
setButton(DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok), this);
}
@Override
public void onClick(final DialogInterface dialog, final int which)
{
// ONLY call the super method in the positive case...
if (which == DialogInterface.BUTTON_POSITIVE)
{
super.onClick(dialog, which);
}
callbackHelper.dialogButtonPressHandled = true;
}
@Override
public void onBackPressed()
{
getButton(DialogInterface.BUTTON_NEGATIVE).performClick();
}
// Need this so we can both pass callbackHelper to the super class and save it off as a variable.
private DatePickerDialog(final Context context,
final OnDateSetListener callBack,
final int year,
final int monthOfYear,
final int dayOfMonth,
final CallbackHelper callbackHelper)
{
super(context, callbackHelper, year, monthOfYear, dayOfMonth);
this.callbackHelper = callbackHelper;
setButtons(context);
}
/**
* @param context The context the dialog is to run in.
* @param callBack How the parent is notified that the date is set.
* @param year The initial year of the dialog.
* @param monthOfYear The initial month of the dialog.
* @param dayOfMonth The initial day of the dialog.
*/
public DatePickerDialog(final Context context,
final OnDateSetListener callBack,
final int year,
final int monthOfYear,
final int dayOfMonth)
{
this(context, callBack, year, monthOfYear, dayOfMonth, new CallbackHelper(callBack));
}
// Need this so we can both pass callbackHelper to the super class and save it off as a variable.
private DatePickerDialog(final Context context, final int theme, final OnDateSetListener listener, final int year,
final int monthOfYear, final int dayOfMonth, final CallbackHelper callbackHelper)
{
super(context, theme, callbackHelper, year, monthOfYear, dayOfMonth);
this.callbackHelper = callbackHelper;
setButtons(context);
}
/**
* @param context The context the dialog is to run in.
* @param theme the theme to apply to this dialog
* @param listener How the parent is notified that the date is set.
* @param year The initial year of the dialog.
* @param monthOfYear The initial month of the dialog.
* @param dayOfMonth The initial day of the dialog.
*/
public DatePickerDialog(final Context context, final int theme, final OnDateSetListener listener, final int year,
final int monthOfYear, final int dayOfMonth)
{
this(context, theme, listener, year, monthOfYear, dayOfMonth, new CallbackHelper(listener));
}
}
TimePickerDialog:
package snappy_company_name_here;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.TimePicker;
/**
* This is a modified version of TimePickerDialog that correctly handles cancellation behavior since it's broken on jellybean and
* kitkat date pickers.
*
* Here is the bug: http://code.google.com/p/android/issues/detail?id=34833
* Here is an SO post with a bunch of details: http://stackoverflow.com/questions/11444238/jelly-bean-datepickerdialog-is-there-a-way-to-cancel
*
* @author stuckj, created on 5/5/15.
*/
public class TimePickerDialog extends android.app.TimePickerDialog implements DialogInterface.OnClickListener
{
final CallbackHelper callbackHelper;
// NOTE: Must be static since we're using it in a super constructor call. Which is annoying, but necessary
private static class CallbackHelper implements OnTimeSetListener
{
private final OnTimeSetListener callBack;
private boolean dialogButtonPressHandled = false; // To prevent setting the date when the dialog is dismissed...
// NOTE: Must be static since we're using it in a super constructor call. Which is annoying, but necessary
public CallbackHelper(final OnTimeSetListener callBack)
{
this.callBack = callBack;
}
@Override
public void onTimeSet(final TimePicker view, final int hourOfDay, final int minute)
{
if (!dialogButtonPressHandled && (callBack != null))
{
callBack.onTimeSet(view, hourOfDay, minute);
}
}
}
/**
* Sets the positive and negative buttons to use the dialog callbacks we define.
*/
private void setButtons(final Context context)
{
setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(android.R.string.cancel), this);
setButton(DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok), this);
}
@Override
public void onClick(final DialogInterface dialog, final int which)
{
// ONLY call the super method in the positive case...
if (which == DialogInterface.BUTTON_POSITIVE)
{
super.onClick(dialog, which);
}
callbackHelper.dialogButtonPressHandled = true;
}
@Override
public void onBackPressed()
{
getButton(DialogInterface.BUTTON_NEGATIVE).performClick();
}
// Need this so we can both pass callbackHelper to the super class and save it off as a variable.
private TimePickerDialog(final Context context,
final OnTimeSetListener callBack,
final int hourOfDay, final int minute, final boolean is24HourView, final CallbackHelper callbackHelper)
{
super(context, callbackHelper, hourOfDay, minute, is24HourView);
this.callbackHelper = callbackHelper;
setButtons(context);
}
/**
* @param context Parent.
* @param callBack How parent is notified.
* @param hourOfDay The initial hour.
* @param minute The initial minute.
* @param is24HourView Whether this is a 24 hour view, or AM/PM.
*/
public TimePickerDialog(final Context context,
final OnTimeSetListener callBack,
final int hourOfDay, final int minute, final boolean is24HourView)
{
this(context, callBack, hourOfDay, minute, is24HourView, new CallbackHelper(callBack));
}
// Need this so we can both pass callbackHelper to the super class and save it off as a variable.
private TimePickerDialog(final Context context, final int theme, final OnTimeSetListener callBack, final int hourOfDay,
final int minute, final boolean is24HourView, final CallbackHelper callbackHelper)
{
super(context, theme, callbackHelper, hourOfDay, minute, is24HourView);
this.callbackHelper = callbackHelper;
setButtons(context);
}
/**
* @param context Parent.
* @param theme the theme to apply to this dialog
* @param callBack How parent is notified.
* @param hourOfDay The initial hour.
* @param minute The initial minute.
* @param is24HourView Whether this is a 24 hour view, or AM/PM.
*/
public TimePickerDialog(final Context context, final int theme, final OnTimeSetListener callBack, final int hourOfDay,
final int minute, final boolean is24HourView)
{
this(context, theme, callBack, hourOfDay, minute, is24HourView, new CallbackHelper(callBack));
}
}
答案 15 :(得分:0)
我使用Lambda表达式使用ClearButton的工作版本:
public class DatePickerFragment extends DialogFragment {
private OnDateSelectListener dateSelectListener;
private OnDateClearListener dateClearListener;
public void setDateSelectListener(OnDateSelectListener dateSelectListener) {
this.dateSelectListener = dateSelectListener;
}
public void setDateClearListener(OnDateClearListener dateClearListener) {
this.dateClearListener = dateClearListener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
// Create a new instance of DatePickerDialog and return it
DatePickerDialog dialog = new DatePickerDialog(getActivity(), null, year, month, day);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setTitle("Select Date");
dialog.setButton(BUTTON_POSITIVE, ("Done"), (dialog1, which) -> {
DatePicker dp = dialog.getDatePicker();
dialog.dismiss();
dateSelectListener.onDateSelect(dp.getYear(), dp.getMonth(), dp.getDayOfMonth());
});
dialog.setButton(BUTTON_NEUTRAL, ("Clear"), (dialog1, which) -> {
dialog.dismiss();
dateClearListener.onDateClear();
});
dialog.setButton(BUTTON_NEGATIVE, ("Cancel"), (dialog1, which) -> {
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.cancel();
}
});
dialog.getDatePicker().setCalendarViewShown(false);
return dialog;
}
public interface OnDateClearListener {
void onDateClear();
}
public interface OnDateSelectListener {
void onDateSelect(int year, int monthOfYear, int dayOfMonth);
}
}
答案 16 :(得分:0)
对于TimePickerDialog,解决方法如下:
TimePickerDialog createTimePickerDialog(Context context, int themeResId, TimePickerDialog.OnTimeSetListener orignalListener,
int hourOfDay, int minute, boolean is24HourView) {
class KitKatTimeSetListener implements TimePickerDialog.OnTimeSetListener {
private int hour;
private int minute;
private KitKatTimeSetListener() {
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
this.hour = hourOfDay;
this.minute = minute;
}
private int getHour() { return hour; }
private int getMinute() {return minute; }
};
KitKatTimeSetListener kitkatTimeSetListener = new KitKatTimeSetListener();
TimePickerDialog timePickerDialog = new TimePickerDialog(context, themeResId, kitkatTimeSetListener, hourOfDay, minute, is24HourView);
timePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok), (dialog, which) -> {
timePickerDialog.onClick(timePickerDialog, DialogInterface.BUTTON_POSITIVE);
orignalListener.onTimeSet(new TimePicker(context), kitkatTimeSetListener.getHour(), kitkatTimeSetListener.getMinute());
dialog.cancel();
});
timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(android.R.string.cancel), (dialog, which) -> {
dialog.cancel();
});
return timePickerDialog;
}
我将所有事件委托给包装器KitKatSetTimeListener,并且只有在单击BUTTON_POSITIVE时才会激活回原始OnTimeSetListener。
答案 17 :(得分:0)
在测试了这里发布的一些消息之后,我个人认为这个解决方案是最简单的。 我在DatePickerDialog构造函数中传递“null”作为我的侦听器,然后当我单击“确定”按钮时,我调用了我的onDateSearchSetListener:
datePickerDialog = new DatePickerDialog(getContext(), null, dateSearch.get(Calendar.YEAR), dateSearch.get(Calendar.MONTH), dateSearch.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setCancelable(false);
datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Debug", "Correct");
onDateSearchSetListener.onDateSet(datePickerDialog.getDatePicker(), datePickerDialog.getDatePicker().getYear(), datePickerDialog.getDatePicker().getMonth(), datePickerDialog.getDatePicker().getDayOfMonth());
}
});
datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Debug", "Cancel");
dialog.dismiss();
}
});
答案 18 :(得分:-1)
我知道这篇文章已经在这里待了将近一年,但我想我应该发布我的发现。 您仍然可以保留监听器(而不是将其设置为仔细考虑)并且仍然按预期工作。关键是隐式设置“确定”或(和)“取消”按钮。我测试了它,它对我很感激。听众不会被解雇两次。
看看这个例子,
private void setTime(){
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
final TimePickerDialog timepicker = new TimePickerDialog(this.getActivity(),
timePickerListener,
hour,
minute,
DateFormat.is24HourFormat(getActivity()));
timepicker.setButton(DialogInterface.BUTTON_POSITIVE, "Print", new
android.content.DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which) {
print = true;
timepicker.dismiss();
}
});
timepicker.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new
android.content.DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
print = false;
timepicker.dismiss();
}
});
timepicker.setCancelable(false);
timepicker.show();
}