我是Android编程新手。请帮忙。
我正在使用Fragment,点击DatePickerDialog
创建材质设计EditText
。麻烦是它被设置为当前日期(由我设置)。但是,如果用户必须选择过去的日期...比如,10年前,用户必须滚动每个月,这是痛苦的。例如如下所示:
有没有办法让你选择一年?这样用户就可以导航到年份。
答案 0 :(得分:2)
我认为这是因为你没有最低日期或格言日期。如果您尝试使用DatePicker创建自定义对话框,则它可以正常工作。我有一个与Edittext相同的例子。当用户单击Edittext时,我会调用此方法。
private void showDateDialog() {
mLayoutInflater = getLayoutInflater();
mCustomDatePicker = mLayoutInflater.inflate(R.layout.custom_date_picker, null);
mDatePicker = (DatePicker) mCustomDatePicker.findViewById(R.id.datePicker);
mDatePicker.setMaxDate(Calendar.getInstance().getTime().getTime());
mCalendar = Calendar.getInstance();
mDialog = new AlertDialog.Builder(this);
mDialog.setView(mCustomDatePicker);
mDialog.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mCalendar.set(mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
mBirthdayEdit.setText(mFormatDate.format(mCalendar.getTime()));
}
}).setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
mDialog.create();
mDialog.show();
}
你布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<DatePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/datePicker"
android:layout_gravity="center_horizontal"
android:spinnersShown="true"
android:calendarViewShown="false"
android:layout_alignParentTop="true"/>
</RelativeLayout>
答案 1 :(得分:0)
public void setMaxDate() {
final Calendar currentDate = Calendar.getInstance();
date = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int
monthOfYear, int dayOfMonth) {
date.set(year, monthOfYear, dayOfMonth);
//use this date as per your requirement
}
};
DatePickerDialog datePickerDialog = new
DatePickerDialog(EditProfileActivity.this, dateSetListener,
currentDate.get(Calendar.YEAR),
currentDate.get(Calendar.MONTH ),
currentDate.get(Calendar.DAY_OF_MONTH));
// Limiting access to past dates in the step below:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -14);
datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
datePickerDialog.show();
}
this is working code ,you can set 14 year ago max date
Enjoy...!
Calendar date;
答案 2 :(得分:0)