如何在datepickerdialog android中选择年份? [英] how to select year in datepickerdialog android?

查看:412
本文介绍了如何在datepickerdialog android中选择年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android编程的新手。请帮忙。

I am new to Android programming. Please help.

我正在使用Fragment,通过单击 EditText DatePickerDialog c $ c>。麻烦是将其设置为当前日期(由我设置)。但是,如果用户必须选择过去(例如10年前)的日期,则用户必须每月滚动一次,这很痛苦。例如如下所示:

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.

推荐答案

我认为那是因为您没有最低日期或最高日期。如果您尝试使用DatePicker创建一个自定义对话框,它将起作用。我有一个与Edittext相同的示例。当用户单击Edittext时,我调用此方法。

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();
}

您的布局

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

这篇关于如何在datepickerdialog android中选择年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆