不显示android timePicker对话框的当前时间

时间:2016-02-02 13:32:50

标签: java android android-edittext timepicker

我在我的应用中有一个时间editText。点击它后,它会弹出TimePicker dialog

    timeIn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { //shows timePick
                    isEdited=true;
                    Fragment fragDialog = getSupportFragmentManager().findFragmentByTag("TimePicker");
                    if (fragDialog == null) { // Fragment not added
                        tp.setFlag(TimePick.FLAG_START_DATE);
                        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                        tp.show(ft, "TimePicker");
                    } else {

                        // already active
                    }
                }
            });


  public static class TimePick extends android.support.v4.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {

        public static final int FLAG_START_DATE = 00;
        public static final int FLAG_END_DATE = 01;
        private int flag = 00;

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);
            return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
        }

        public void setFlag(int i) {
            flag = i;
        }


        @Override
        public void onTimeSet(TimePicker view, int hourofDay, int minute) {

            if (flag == FLAG_START_DATE) {
                timeIn.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
                TimeIn = timeIn.getText().toString();
            }
            if (flag == FLAG_END_DATE) {
                timeOut.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
                TimeOut = timeOut.getText().toString();
            }



        }

    }

所选时间可以是setText中的timeIn。但是,当我再次单击time editText时,timePicker dialog弹出当前时间。是否可以显示所选时间而不是当前时间?如果是第一次单击,则仅显示当前时间。

1 个答案:

答案 0 :(得分:1)

这不是你在这里做的事

final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));

您正在将当前时间传递给对话框,这就是显示当前时间的原因。

<强>解决方案:

保存选定的时间并在创建对话框时将其传递给类,并在此代码中使用该时间。如果您没有任何选定的时间,请使用当前时间。一个小if-else块和一些变量就可以了。

在外部类中定义这些变量。

public static int inHour = -1;
public static int inMin = -1;
public static int outHour = -1;
public static int outMin = -1;

像这样更新onTimeSet方法

if (flag == FLAG_START_DATE) {
    inHour = hourofDay;
    inMin = minute;
    timeIn.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
    TimeIn = timeIn.getText().toString();
}
else if (flag == FLAG_END_DATE) {
    outHour = hourofDay;
    outMin = minute;
    timeOut.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
    TimeOut = timeOut.getText().toString();
}

onCreateDialog中的当前代码替换为

final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
if((flag == FLAG_START_DATE) && inHour != -1 && inMin != -1) 
{
     hour = inHour;
     min = inMin;
}
else if((flag == FLAG_END_DATE) && outHour != -1 && outMin != -1) 
{
     hour = outHour;
     min = outMin;
}

return new TimePickerDialog(getActivity(), this, hour, min, DateFormat.is24HourFormat(getActivity()));