大家好!我正在android上建立一个酒店预订应用程序,我的问题是当我通过datepicker在登记入住的editext中选择一个日期时,退房预订中的月份显示在datepicker的下个月。我该如何解决这个问题?谢谢。这是我的日期选择器代码行。
etin = (EditText) findViewById(R.id.etcheckin);
etin.setInputType(InputType.TYPE_NULL);
etout = (EditText) findViewById(R.id.etcheckout);
etout.setInputType(InputType.TYPE_NULL);
etin.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
showTruitonDatePickerDialog(view);
}
});
etout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToDatePickerDialog(view);
}
});
}
public void showTruitonDatePickerDialog(View v){
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public void showToDatePickerDialog(View v){
DialogFragment newFragment = new ToDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.Theme_AppCompat_DayNight_Dialog_Alert, this, year, month, day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.set(2018,11 ,31);
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
return datePickerDialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, month, day, 0, 0, 0);
int mont = month + 1;
etin.setText(mont + "/" + day+ "/" + year);
// etin.setText((etin.getmonth + 1) + );
}
}
public static class ToDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
// Calendar startDateCalendar=Calendar.getInstance();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
String getfromdate = etin.getText().toString();
String getfrom[] = getfromdate.split("/");
int year,month,day;
year= Integer.parseInt(getfrom[2]);
month = Integer.parseInt(getfrom[0]);
day = Integer.parseInt(getfrom[1]);
final Calendar c = Calendar.getInstance();
c.set(year, month, day + 1);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.Theme_AppCompat_DayNight_Dialog_Alert, this, year, month, day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.set(2018,11, 31);
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
return datePickerDialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, month, day, 0, 0, 0);
etout.setText(month + "/" + day + "/" + year);
}
}
答案 0 :(得分:0)
使用此代替您计算日期并拆分为变量。
private void chooseDate(final EditText edtdate) {
Calendar calDateGiven = Calendar.getInstance();
int year = calDateGiven.get(Calendar.YEAR);
int month = calDateGiven.get(Calendar.MONTH);
int day = calDateGiven.get(Calendar.DATE);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, (view, year1, monthOfYear, dayOfMonth) -> {
calDateGiven = Calendar.getInstance();
calDateGiven.set(Calendar.YEAR, year1);
calDateGiven.set(Calendar.MONTH, monthOfYear);
calDateGiven.set(Calendar.DATE, dayOfMonth);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
String selectedDate = simpleDateFormat.format(calDateGiven.getTime());
edtdate.setText(selectedDate);
}, year, month, day);
try {
Calendar cal = Calendar.getInstance();
datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());
} catch (Exception e) {
Toast.makeText(this, getString(R.string.signupFailed), Toast.LENGTH_SHORT).show();
Log.e("on chooseDate", "Add my contacts", e);
}
datePickerDialog.show();
}