how to select year in datepickerdialog android?

橙三吉。 提交于 2020-02-02 06:38:09

问题


I am new to Android programming. Please help.

I am using Fragment that creates Material design DatePickerDialog on click of EditText. Trouble is it is set to current date (set by me). But, if user has to select the date in the past ... say, 10 years ago, user has to scroll each month which is painful. e.g. shown below:

Is there a way to make your select the year? This way user can navigate to the year.


回答1:


I think than that is because you don't have a minimum date or maxim date. If you try to create a custom dialog with DatePicker, it works. I have a same example with an Edittext. I call this method when the user to click in 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();
}

You layout

<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>



回答2:


At least on Android 7, you can click on the year and get a year selection dialog:




回答3:


    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...!



来源:https://stackoverflow.com/questions/32584417/how-to-select-year-in-datepickerdialog-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!