来自另一个DatePicker的DatePicker的setMinDate

时间:2017-10-09 12:19:17

标签: android android-datepicker

我正在为酒店(个人学习项目)开发应用程序。在"立即预订"页面I显示2个EditText字段。一个用于登记入住日期,另一个用于退房日期。

我已将DatePicker设置为仅使用setMinDate()显示当前和未来日期。但我想要的是签出DatePicker应该显示(签到日期+ 1)作为可选日期。

例如,如果用户选择登记日期为24/10/2017,则结账DatePicker应显示2017年10月25日的日期。

BookFragment.java

public void onStart() {
    super.onStart();

    EditText inDate = v.findViewById(R.id.checkInDate);
    inDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            DateDialog dialog = new DateDialog(view);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            dialog.show(ft, "Select Check-In Date");
        }
    });

    EditText outDate = v.findViewById(R.id.checkOutDate);
    outDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            EditText inDateText = v.findViewById(R.id.checkInDate);
            if (TextUtils.isEmpty(inDateText.getText().toString())) {
                Toast.makeText(getContext(), "Select Check-in Date first", Toast.LENGTH_SHORT).show();
            } else {
                DateDialog dialog = new DateDialog(view);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                dialog.show(ft, "Select Check-Out Date");
            }
        }
    });

}

DateDialog.java

@SuppressLint("ValidFragment")
public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {

EditText dateText;
int cyear, cmonth, cday;

public DateDialog(View view) {
    dateText = (EditText) view;
}

@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {

    DatePickerDialog date = new DatePickerDialog(getActivity(), this, cyear, cmonth, cday);
    final Calendar c = Calendar.getInstance();
    Date currentDate = c.getTime();
    cyear = c.get(Calendar.YEAR);
    cmonth = c.get(Calendar.MONTH);
    cday = c.get(Calendar.DAY_OF_MONTH);
    date.getDatePicker().setMinDate(currentDate.getTime());
    return date;

}

public void onDateSet(DatePicker view, int year, int month, int day) {
    String date = day + "/" + (month + 1) + "/" + year;
    dateText.setText(date);
}

是book.xml

<EditText
        android:id="@+id/checkInDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:focusable="false"
        android:hint="@string/check_in_date"
        android:inputType="date" />

    <EditText
        android:id="@+id/checkOutDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:focusable="false"
        android:hint="@string/check_out_date"
        android:inputType="date" />

由于EditText都调用相同的DateDialog类,如何获取签入日期并将其用于setMinDate()以进行签出?

现在,我正在做的解决方法是从EditText获取日期字符串,将其解析为Date并执行相同的检出。当用户点击&#34; Book&#34;按钮,它使用

检查结账时间是否早于办理登机手续
if (odate.before(idate)) {
        outDateText.setError("Check-Out must be after Check-In");
        Toast.makeText(getContext(), "Check-Out Date must be after Check-In Date", Toast.LENGTH_SHORT).show();
        flag = 1;
    }

这样可以完成工作,但我希望在入住日期之前禁用日期。

1 个答案:

答案 0 :(得分:0)

Date

String string_date = "24/10/2017";

然后,将一天增加到您当前的日期

Calendar cal = Calendar.getInstance(); 
cal.setTime(string_date); 
cal.add(Calendar.DATE, 1);
checkOutDate = cal.getTimeInMillis();

然后将minDate设置为datePicker

datePicker.setMinDate(checkOutDate - 1000);

修改

global 类中创建long dateToSet变量DateDialog

喜欢 - long dateToSet;

更改constructor
public DateDialog(View view) {
dateText = (EditText) view;
}

public DateDialog(View view, long dateToSet) {
dateText = (EditText) view;
//then update the value of dateToSet here
this.dateToSet = dateToSet
}

然后为 Editext 1 使用此

else {
            Date currentDate = c.getTime();
            cyear = c.get(Calendar.YEAR);
            cmonth = c.get(Calendar.MONTH);
            cday = c.get(Calendar.DAY_OF_MONTH);
            DateDialog dialog = new DateDialog(view,currentDate.getTime());
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            dialog.show(ft, "Select Check-Out Date");
        }

Editext 2 使用此

//String string_date = "24/10/2017";
//getting current date from editText 1
String string_date = editText.getText().toString();
//check the string must not be null
//then, increase one day to your current date

Calendar cal = Calendar.getInstance(); 
cal.setTime(string_date); 
cal.add(Calendar.DATE, 1);
checkOutDate = cal.getTimeInMillis();
DateDialog dialog = new DateDialog(view,checkOutDate);
FragmentTransaction ft = getFragmentManager().beginTransaction();
            dialog.show(ft, "Select Check-Out Date");

然后您的 onCreateDialog 必须

public Dialog onCreateDialog(Bundle savedInstanceState) {

DatePickerDialog date = new DatePickerDialog(getActivity(), this, cyear, cmonth, cday);
final Calendar c = Calendar.getInstance();
date.getDatePicker().setMinDate(dateToSet);
return date;

}

可以帮到你