简体   繁体   中英

Not display current time for android timePicker dialog

I have a timeIn editText in my app. When it is clicked, it will pop up 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();
            }



        }

    }

The time selected can be setText in timeIn . But when I click the timeIn editText again, the timePicker dialog popup with the current time . Is it possible to make it display the selected time instead of current time ? If it is first time click, then only show current time.

Isn't that what you are doing here

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()));

You are passing the current time to the dialog, that's why its showing the current time.

Solution:

Save the selected time and pass that to the class while creating the dialog and use that time in this code. If you don't have any selected time then use the current time. A little if-else block and some variables will do the trick.

Define these variable in outer class.

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

update onTimeSet method like this

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();
}

Replace your current code in onCreateDialog with

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()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM