如何从时间选择器对话框设置时间?

时间:2016-04-11 07:29:08

标签: java android android-timepicker

我想设置两次,即提醒时间和到期时间。我已经为警报时间和截止时间创建了两个对话框。我试图在不同的日期对象中设置两个时间。当我改变警报时间时,到期时间也会改变。我希望以不同的方式保存这两个值。

    private int ALERT_TIME_DIALOG, ALERT_DATE_DIALOG,DUE_TIME_DIALOG,DUE_DATE_DIALOG;
      @Override
        public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) {
            String hourString = hourOfDay < 10 ? "0"+hourOfDay : ""+hourOfDay;
            String minuteString = minute < 10 ? "0"+minute : ""+minute;
            String secondString = second < 10 ? "0"+second : ""+second;
            String aTime,dTime;
            SimpleDateFormat  df = new SimpleDateFormat("hh:mm a");

            if(ALERT_TIME_DIALOG == 2) {
                Date alertDate = new Date();
                mCalendar = Calendar.getInstance();
                mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                mCalendar.set(Calendar.MINUTE, minute);
                mCalendar.set(Calendar.SECOND,0);
                alertDate = mCalendar.getTime();
                aTime = df.format(alertDate);
                alertTime.setText(aTime);
            }

            if(DUE_TIME_DIALOG  == 4){
                Date dueDate = new Date();
                mCalendar = Calendar.getInstance();
                mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                mCalendar.set(Calendar.MINUTE, minute);
                mCalendar.set(Calendar.SECOND,0);
                dueDate = mCalendar.getTime();
                dTime = df.format(dueDate);
                dueTime.setText(dTime);
            }
        }


     alertTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ALERT_TIME_DIALOG = 2;

                    Calendar now = Calendar.getInstance();

                    TimePickerDialog tpd = TimePickerDialog.newInstance(
                            AddTaskActivity.this,
                            now.get(Calendar.HOUR_OF_DAY),
                            now.get(Calendar.MINUTE),
                            mHoursMode
                    );

                    tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialogInterface) {
                            Log.d("TimePicker", "Dialog was cancelled");
                        }
                    });
                    tpd.show(getFragmentManager(), "Timepickerdialog");
                }
            });


     dueTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    DUE_TIME_DIALOG = 4;

                    Calendar now = Calendar.getInstance();

                    TimePickerDialog tpd = TimePickerDialog.newInstance(
                            AddTaskActivity.this,
                            now.get(Calendar.HOUR_OF_DAY),
                            now.get(Calendar.MINUTE),
                            mHoursMode
                    );

                    tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialogInterface) {
                            Log.d("TimePicker", "Dialog was cancelled");
                        }
                    });
                    tpd.show(getFragmentManager(), "Timepickerdialog");
                }
            });

这里有什么问题。 谢谢你..

1 个答案:

答案 0 :(得分:0)

修改您的代码如下:您已为“警报”和“截止日期”对话框设置了两个不同的标记。因此,当您重复该过程时,都会设置该值,并且在onTimeSet方法中,如果语句执行,则为

private int TIME_PICKER_DIALOG_TAG;
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) {
    String hourString = hourOfDay < 10 ? "0"+hourOfDay : ""+hourOfDay;
    String minuteString = minute < 10 ? "0"+minute : ""+minute;
    String secondString = second < 10 ? "0"+second : ""+second;

    SimpleDateFormat  df = new SimpleDateFormat("hh:mm a");
    mCalendar = Calendar.getInstance();
    mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    mCalendar.set(Calendar.MINUTE, minute);
    mCalendar.set(Calendar.SECOND,0);

    Date date = mCalendar.getTime();
    if(TIME_PICKER_DIALOG_TAG == 2) {
        alertTime.setText(df.format(date));
    }else if(TIME_PICKER_DIALOG_TAG  == 4){
        dueTime.setText(df.format(date));
    }
}


alertTime.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        TIME_PICKER_DIALOG_TAG = 2;

        Calendar now = Calendar.getInstance();

        TimePickerDialog tpd = TimePickerDialog.newInstance(
            AddTaskActivity.this,
            now.get(Calendar.HOUR_OF_DAY),
            now.get(Calendar.MINUTE),
            mHoursMode
        );

        tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            Log.d("TimePicker", "Dialog was cancelled");
        }
        });
        tpd.show(getFragmentManager(), "Timepickerdialog");
    }
});


dueTime.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        TIME_PICKER_DIALOG_TAG = 4;

        Calendar now = Calendar.getInstance();

        TimePickerDialog tpd = TimePickerDialog.newInstance(
            AddTaskActivity.this,
            now.get(Calendar.HOUR_OF_DAY),
            now.get(Calendar.MINUTE),
            mHoursMode
        );

        tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            Log.d("TimePicker", "Dialog was cancelled");
        }
        });
        tpd.show(getFragmentManager(), "Timepickerdialog");
    }
});