我可以在timepickerdalog中将最短时间设置为当前时间吗?

时间:2015-05-26 06:37:59

标签: android

我使用setMinDate()在datePickerDialog中将最小日期设置为当前日期。现在我想将相同的时间设置为timePickerDialog ..这可能吗?如果是,请帮助..

public class AddTasks extends Activity {

TaskForm activityForm = new TaskForm();

private Button pPickDate;
private int pYear;
private int pMonth;
private int pDay;
public String date;
public String Loc;
public double lat;
public double lon;

private Button pPickTime;
private int mHour;
private int mMinute;
public String time;

private TimePickerDialog timePickerDialog;
private DatePickerDialog datePickerDialog;

Date selectedDate;


/** This integer will uniquely define the dialog to be used for displaying date picker.*/
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;

/** Callback received when the user "picks" a date in the dialog */
private DatePickerDialog.OnDateSetListener pDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {
                pYear = year;
                pMonth = monthOfYear;
                pDay = dayOfMonth;
               updateDate();
                displayDateToast();
            }
        };

private TimePickerDialog.OnTimeSetListener mTimeListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hour, int minute) {
                mHour = hour;
                mMinute = minute;
                updateTime();
                displayTimeToast();
            }
        };

/** Updates the date in the TextView */
private void updateDate() {
  /**  pDisplayDate.setText( */ date =
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(pDay).append("/")
                    .append(pMonth + 1).append("/")
                    .append(pYear).append(" ").toString();
   // date = pDisplayDate.getText().toString();

}

private void updateTime() {
   /** pDisplayTime.setText( */ time =
            new StringBuilder()
                    .append(mHour).append(":")
                    .append(mMinute).append("").toString();
}

/** Displays a notification when the date is updated */
private void displayDateToast() {
    //Toast.makeText(this, new StringBuilder().append("Date choosen is ").append(date),  Toast.LENGTH_LONG).show();
    activityForm.setTaskDateInput(date);
}

private void displayTimeToast() {
  //  Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(time),  Toast.LENGTH_LONG).show();
    activityForm.setTaskTimeInput(time);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_task);
    init();

    /** Capture our View elements */
    //pDisplayDate = (TextView) findViewById(R.id.displayDate);
    pPickDate = (Button) findViewById(R.id.pickDate);

    /** Listener for click event of the button */
    pPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

   /** Capture our View elements */
    //pDisplayTime = (TextView) findViewById(R.id.displayTime);
    pPickTime = (Button) findViewById(R.id.pickTime);


    /** Listener for click event of the button */
    pPickTime.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });

    /** Get the current date and time */
    final Calendar cal = Calendar.getInstance();
    pYear = cal.get(Calendar.YEAR);
    pMonth = cal.get(Calendar.MONTH);
     pDay = cal.get(Calendar.DAY_OF_MONTH);

    mHour = cal.get(Calendar.HOUR_OF_DAY);
    mMinute = cal.get(Calendar.MINUTE);
}

/** Create a new dialog for date and time picker */
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            datePickerDialog =  new DatePickerDialog(this,
                    pDateSetListener,
                    pYear, pMonth, pDay);
            Date dt = new Date();
            Calendar calendar = new Calendar();
            calendar.setTime(dt);
            mHour = calendar.get(Calendar.HOUR_OF_DAY);
            mMinute = calendar.get(Calendar.MINUTE);
            datePickerDialog.getDatePicker().setMinDate(dt.getTime() - 10000);
           // datePickerDialog.getDatePicker().setMinDate(new Date().getTime() - 10000);
            return datePickerDialog;

        case TIME_DIALOG_ID:
            timePickerDialog = new TimePickerDialog(this,
                    mTimeListener,
                    mHour,mMinute,true) ;
             return timePickerDialog;


    }
    return null;
}

我已经通过添加整个代码编辑了我的帖子..

我有2个按钮分别用于datepickerdialog和timepickerdialog ..我想要的是当我点击按钮并打开对话框时,用户不应该在对话框中设置过去的日期或时间,即对话框应显示当前日期和时间..通过Datepickerdialog上的setMinDate()可以做日期但是时间没有这样的方法..我怎么能这样做..?

1 个答案:

答案 0 :(得分:0)

尝试这一点,当你将datePickerDialog Date对象设置为最小日期时,你会得到小时和分钟,

protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            datePickerDialog =  new DatePickerDialog(this,
                    pDateSetListener,
                    pYear, pMonth, pDay);
            Date dt = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dt);
            mHour = calendar.get(Calendar.HOUR_OF-DAY);
            mMinute = calendar.get(Calendar.MINUTE);
            datePickerDialog.getDatePicker().setMinDate(dt.getTime() - 10000);
            return datePickerDialog;

        case TIME_DIALOG_ID:
            timePickerDialog = new TimePickerDialog(this,
                    mTimeListener,
                    mHour,mMinute,true) ;
             return timePickerDialog;


    }
    return null;
}