14

当我调用显示对话框时,我的应用程序出现了这个问题,它以某种方式被调用了两次。这只发生在 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);

    }
};
4

8 回答 8

11

根据您的代码,这些方法都不会被调用,因为您从不使用TimePickerDialogs.

话虽如此,有一个与DatePickerDialog/的行为相关的已知问题TimePickerDialog可能相关:http ://code.google.com/p/android/issues/detail?id=34833

于 2012-12-11T12:17:25.007 回答
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();
}
于 2013-08-06T14:36:18.797 回答
2

如果您查看 android 源代码,似乎 onTimeSet() 在 onStop() 期间被调用。该对话框还在其 onClick() 方法中调用 onTimeSet()。

为了解决这个问题,我必须覆盖对话框关闭和取消功能以设置一个布尔值,我检查是否从按钮单击或关闭/取消调用 onTimeSet

于 2013-07-26T13:24:20.937 回答
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

于 2014-11-14T09:53:18.663 回答
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();

}
于 2012-12-10T21:07:30.053 回答
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();
于 2014-09-04T18:35:43.623 回答
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()));
于 2015-01-30T09:08:42.433 回答
-1

我遇到了两次显示时间选择器的问题。我已经覆盖了 TimePickerDialog 的 onStop() 并解决了这个问题。此外,您不应在覆盖的 onStop() 中调用 super.onStop()。

@Override
public void onStop() {
    //super.onStop();
    Log.d(TAG, "onStop of starttime TimePickerDialog.");
}
于 2013-08-07T10:20:13.857 回答