当我打电话给我显示一个对话框时,我的应用程序遇到了这个问题,不知何故,它被调用了两次。这仅适用于Android 4.1及更高版本。较低版本工作正常,所以我不认为这是任何代码问题。
你有没有听说过以前遇到过这个问题?
这里是代码:
Button edit = (Button) ad.findViewById(R.id.editBtn);
edit.setTypeface(roboto);
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setDate();
ad.dismiss();
}
});
ad.show();
ad.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
shiftsActivity.setPressed(true);
}
});
}
public void setDate() {
// Initialize and open the set date dialog
DatePickerDialog setDateDialog = new DatePickerDialog(Shifts.this,
datePickerListener, dateAndTime.get(Calendar.YEAR),
dateAndTime.get(Calendar.MONTH),
dateAndTime.get(Calendar.DAY_OF_MONTH));
setDateDialog.setTitle("Set Date");
setDateDialog.show();
}
public void setStartTime() {
TimePickerDialog setStartTimeDialog = new TimePickerDialog(Shifts.this,
startTimePicker, dateAndTime.get(Calendar.HOUR),
dateAndTime.get(Calendar.MINUTE), true);
setStartTimeDialog.setTitle("Started At:");
setStartTimeDialog.show();
}
public void setEndTime() {
TimePickerDialog setEndTimeDialog = new TimePickerDialog(Shifts.this,
setEndTime, dateAndTime.get(Calendar.HOUR),
dateAndTime.get(Calendar.MINUTE), true);
setEndTimeDialog.setTitle("Ended:");
setEndTimeDialog.show();
}
TimePickerDialog.OnTimeSetListener startTimePicker = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
startIntHours = hourOfDay;
startIntMinutes = minute;
editStartTime = String.format("%02d", hourOfDay) + ":"
+ String.format("%02d", minute);
setEndTime();
}
};
TimePickerDialog.OnTimeSetListener setEndTime = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
finishIntHours = hourOfDay;
finsihIntMinutes = minute;
if (finishIntHours < startIntHours) {
finishIntHours = finishIntHours + Utility.HOURS_TIME_UNIT;
}
if (finsihIntMinutes < startIntMinutes) {
finsihIntMinutes = finsihIntMinutes + Utility.MINUTES_TIME_UNIT;
}
totalHours = finishIntHours - startIntHours;
totalMinutes = finsihIntMinutes - startIntMinutes;
Log.i("TotalHours in time picker", "" + totalHours);
Log.i("Totalminute in time picker", "" + totalMinutes);
editEndTime = String.format("%02d", hourOfDay) + ":"
+ String.format("%02d", minute);
replace(Shifts.view, Shifts.position);
}
};
答案 0 :(得分:11)
根据您的代码,这些方法都没有被调用过,因为您从未使用过TimePickerDialogs
。
话虽如此,但有一个与DatePickerDialog
/ TimePickerDialog
行为相关的已知问题可能相关:http://code.google.com/p/android/issues/detail?id=34833
答案 1 :(得分:3)
这是一个基本的解决方案,适用于需要TimeChooserDialog但没有使用基本选项调用两次侦听器的人
public static AlertDialog getTimePickerDialog(Context context, final OnTimeSetListener listener, int hour, int minute, boolean is24HFormat) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final TimePicker timePicker = new TimePicker(context);
timePicker.setIs24HourView(is24HFormat);
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
builder.setView(timePicker);
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if(null != listener) {
listener.onTimeSet(timePicker, timePicker.getCurrentHour(), timePicker.getCurrentMinute());
}
}
});
builder.setNegativeButton(android.R.string.cancel, null);
return builder.create();
}
答案 2 :(得分:2)
如果查看android源代码,看起来在onStop()期间会调用onTimeSet()。该对话框还在其onClick()方法中调用onTimeSet()。
为了解决这个问题,我不得不重写对话框dismiss和取消函数来设置一个布尔值,我检查是否从按钮点击或解除/取消调用onTimeSet
答案 3 :(得分:1)
我对TimePicker也会出现同样的问题。我解决了这个问题。
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int setHour, int setMinute) {
if (view.isShown()) {
// This method will return true only once
}
}
};
iShown():
返回此视图及其所有祖先的可见性
返回 如果此视图及其所有祖先都是可见的,则为True
答案 4 :(得分:0)
我尝试了,如果您仍有问题尝试使用classic singleton
像这样的东西:)详细了解它。
public void setStartTime() {
TimePickerDialog setStartTimeDialog = NULL;
if(setStartTimeDialog == null) {
new TimePickerDialog(Shifts.this, startTimePicker, dateAndTime.get(Calendar.HOUR),dateAndTime.get(Calendar.MINUTE), true);
setStartTimeDialog.setTitle("Started At:");
}
setStartTimeDialog.show();
}
答案 5 :(得分:0)
不要使用内置的TimePickerDialog或DatePickerDialog。而是创建一个自定义对话框,如此处所示。它简单易用,只是有效!
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final TimePicker timePicker = new TimePicker(getActivity());
builder.setTitle("Set Time");
builder.setView(timePicker);
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("OK"), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int minute = timePicker.getCurrentMinute();
int hourOfDay = timePicker.getCurrentHour();
}
});
builder.show();
答案 6 :(得分:0)
我不是违反super.onClose()或制作自定义对话框,而是通过包装真实的侦听器并扩展启用和禁用从包装器转发到真实侦听器的TimePickerDialog来解决它。它不是替代品,但它足够整洁。
public class TimePickerDialogCustom extends TimePickerDialog
{
protected TimePickerDialogOnTimeSetListenerWrapper listenerWrapper;
public TimePickerDialogCustom(Context context,
TimePickerDialogOnTimeSetListenerWrapper listener,
int hourOfDay, int minute, boolean is24HourView)
{
super(context, listener, hourOfDay, minute, is24HourView);
listenerWrapper = listener;
}
@Override
protected void onStop()
{
if (listenerWrapper != null)
listenerWrapper.enabled = false;
super.onStop();
if (listenerWrapper != null)
listenerWrapper.enabled = true;
}
public static final class TimePickerDialogOnTimeSetListenerWrapper implements TimePickerDialog.OnTimeSetListener
{
private boolean enabled = true;
private final TimePickerDialog.OnTimeSetListener delegate;
public TimePickerDialogOnTimeSetListenerWrapper(TimePickerDialog.OnTimeSetListener delegate)
{
this.delegate = delegate;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
if (enabled)
{
this.delegate.onTimeSet(view, hourOfDay, minute);
}
}
}
}
然后使用它:
TimePickerDialogCustom.TimePickerDialogOnTimeSetListenerWrapper listenerWrapper =
new TimePickerDialogCustom.TimePickerDialogOnTimeSetListenerWrapper(realListener);
// Create a new instance of DatePickerDialog and return it
TimePickerDialogCustom dialog = new TimePickerDialogCustom(
getActivity(),
listenerWrapper,
hourOfDay,
minute,
DateFormat.is24HourFormat(getActivity()));
答案 7 :(得分:-1)
我遇到了这个问题,两次显示时间戳。我已经覆盖了TimePickerDialog的onStop()并解决了这个问题。另外,你不应该在你的覆盖onStop()中调用super.onStop()。
@Override
public void onStop() {
//super.onStop();
Log.d(TAG, "onStop of starttime TimePickerDialog.");
}