我在一个datepickers
中使用两个start date
一个用于end date
,一个用于activity
,每当我选择一个月时,它会显示错误的数字。我尝试将+1
添加到月份值,但由于有两个datepickers
,我们必须跳过start date
之前的日期才能进行选择,因此无效。代码如下:
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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;
datePickerDialog = new DatePickerDialog(getActivity(), this, year,
month, day);
return datePickerDialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
cval_from.setText(day + "/" + month + "/" + year);
}
}
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 = cval_from.getText().toString().trim();
String getfrom[] = getfromdate.split("/");
int year, month, day;
year = Integer.parseInt(getfrom[2]);
month = Integer.parseInt(getfrom[1]);
day = Integer.parseInt(getfrom[0]);
final Calendar c = Calendar.getInstance();
c.set(year, month, day + 1);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
return datePickerDialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
cval_to.setText(day + "/" + month + "/" + year);
}
}
此处cval_from
为start date
而cval_to
为end date
,现在如果我在month+1
cval_from
中添加cval_to
日期的选择从下个月开始。例如,如果我为26/1/2018
选择cval_from
,则必须从cval_to
开始选择27/1/2018
。整个月都被跳过了。
有人可以帮忙吗?
答案 0 :(得分:2)
根据developer.android.com月份范围0-11。
所以你需要将它递增1。
public void onDateSet(DatePicker view, int year, int month, int day) {
int selectedMonth = month+1;
cval_to.setText(day + "/" + selectedMonth + "/" + year);
}
答案 1 :(得分:2)
当您使用最初要在DatePicker中设置的方法中的开始日期发送日期以及结束日期时,可以在不同的点击时使用两个日期选择器,并在Click
中调用此方法void selectDate(int mDay ,int mMonth,int mYear) {
DatePickerDialog datePickerDialog = new DatePickerDialog(this,R.style.mydatetheame,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
tv_date.setText((monthOfYear + 1) + "/" + dayOfMonth + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
当然,通过这种方法你将得到一个正确的月份。