从第一个datepicker android studio中选择日期设置第二个datepicker

时间:2018-01-15 02:07:35

标签: android datepicker

我正在创建预订,我的模块首先是日期选择器(登记入住和退房)。我正在尝试所有可以通过日期选择器帮助我的代码。但没有任何反应。我想从我的第一个日期选择器的选定日期设置我的第二个日期选择器mindate

有人可以帮我解决这个问题吗?谢谢。

示例:

  

当前日期= 2018年1月1日   入住日期= 2018年1月24日   Check -out mindate应该是1月25日到更高(可以选择)

 /** DATE PICKER DIALOG _ START **/

    etin = (EditText) findViewById(R.id.etcheckin);
    etin.setInputType(InputType.TYPE_NULL);
    etout = (EditText) findViewById(R.id.etcheckout);
    etout.setInputType(InputType.TYPE_NULL);

    //initialization
    DateFormat dateFormat = new SimpleDateFormat("MMM dd  yyyy", Locale.getDefault());
    final Calendar calendar = Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);
    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);

    //set ng current date
    String departureDatetime = dateFormat.format(calendar.getTime());
    etin.setText(departureDatetime);

    // Determine min date to select in calendar.
    mMinReturnDate = calendar.getTimeInMillis();

    //set ng current date plus 1 day para sa check out
    calendar.add(Calendar.DATE, 1);
    String returnDatetime = dateFormat.format(calendar.getTime());
    etout.setText(returnDatetime);

    etin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            DialogFragment dFragment = new DatePickerFragment();
            // Show the date picker dialog fragment
            dFragment.show(getFragmentManager(), "Date Picker Check In");
            //end


        }
    });

    etout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            DatePickerFragmentCheckOut dFragmentOut = new DatePickerFragmentCheckOut();
            // Show the date picker dialog fragment
            dFragmentOut.show(getFragmentManager(), "Date Picker Check Out");
            //end
        }
    });
}

public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    Calendar c = Calendar.getInstance();
    final Calendar calendar = Calendar.getInstance();
    int day;
    int month;
    int year;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        day = calendar.get(Calendar.DAY_OF_MONTH);
        month = calendar.get(Calendar.MONTH);
        year = calendar.get(Calendar.YEAR);
        DatePickerDialog dpd = new DatePickerDialog(getActivity(), R.style.Theme_AppCompat_DayNight_Dialog_Alert, this, year, month, day);

        c.set(2018, 11, 31);//Year,Month -1,Day
        dpd.getDatePicker().setMaxDate(c.getTimeInMillis());
        dpd.getDatePicker().setMinDate(calendar.getTimeInMillis());
        return dpd;

    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the chosen date
        EditText etin = (EditText) getActivity().findViewById(R.id.etcheckin);

        // Create a Date variable/object with user chosen date
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(0);
        cal.set(year, month, day, 0, 0, 0);
        Date chosenDate = cal.getTime();

        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
        String formattedDate = df.format(chosenDate);

        etin.setText(formattedDate);
    }
}

public static class DatePickerFragmentCheckOut extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    Calendar c = Calendar.getInstance();
    final Calendar calendar = Calendar.getInstance();
    int day;
    int month;
    int year;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        day = calendar.get(Calendar.DAY_OF_MONTH + 1);
        month = calendar.get(Calendar.MONTH);
        year = calendar.get(Calendar.YEAR);
        DatePickerDialog dpdOut = new DatePickerDialog(getActivity(), R.style.Theme_AppCompat_DayNight_Dialog_Alert, this, year, month, day);


        c.set(2018, 11, 31);//Year,Month -1,Day
        dpdOut.getDatePicker().setMaxDate(c.getTimeInMillis());

        Date min = new Date();

        dpdOut.getDatePicker().setMinDate(calendar.getTimeInMillis());
        return dpdOut;

    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the chosen date
        EditText etout = (EditText) getActivity().findViewById(R.id.etcheckout);

        // Create a Date variable/object with user chosen date
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(0);
        cal.set(year, month, day, 0, 0, 0);
        Date chosenDate = cal.getTime();

        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
        String formattedDate = df.format(chosenDate);

        etout.setText(formattedDate);
    }

}


/** DATE PICKER DIALOG _ END **/`

0 个答案:

没有答案