如何在 Android 对话框日期选择器中将默认显示设置为年份

How to set default display as year wise in Android Dialog datepicker

我正在尝试弄清楚如何在 Android 对话框 DatePicker 中设置默认显示年份。我们的场景是,首先用户必须 select 年,然后是月,最后是对话框中的日期 DatePicker。为此,我们需要在打开对话框时打开年份显示。

PFB 图像 link。请参考右图默认打开。

您可以使用日历并减去您想要的年份,例如:

public Dialog onCreateDialog(Bundle saveInstanceState)
{
    final Calendar c = Calendar.getInstance();
    c.add(Calendar.YEAR, -18);
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    return new DatePickerDialog(getActivity(), this, year, month, day);
}

实施您的 activity :

实现DatePickerDialog.OnDateSetListener{

当您单击按钮以获取日期选择器对话框时在 onCreate 中:

calendar2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Initialize a new date picker dialog fragment
        DialogFragment dFragment = new DatePickerFragment();
      //  calendar2.setText(dateDisplay);
        // Show the date picker dialog fragment
        dFragment.show(getFragmentManager(), "Date Picker");
    }
});

在oncreate之外

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
  //  Calendar cal = new GregorianCalendar(year, month, day);
    month = month + 1;
    Log.d ( TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year );
    String date = day + "/" + month + "/" + year;
 //   mDisplayDate.setText ( date );
    setDate(date);
}
private void setDate(String date) {
  //  final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
    ((TextView) findViewById(R.id.calendar2))
            .setText(date);
}




 public static class DatePickerFragment extends DialogFragment implements android.app.DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            /*
                Create a DatePickerDialog using Theme.
                    DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
                        int year, int monthOfYear, int dayOfMonth)
             */
            // DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
//            android.app.DatePickerDialog dpd = new android.app.DatePickerDialog(getActivity(),
//                    AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, this, year, month, day);
          android.app.DatePickerDialog dpd = new android.app.DatePickerDialog(getActivity(),
                            R.style.DatePickerDialogTheme,  (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day);
//            new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,
//                    (DatePickerDialog.OnDateSetListener)
//                            getActivity(), year, month, day)
            // DatePickerDialog THEME_DEVICE_DEFAULT_DARK
            android.app.DatePickerDialog dpd2 = new android.app.DatePickerDialog(getActivity(),
                    AlertDialog.THEME_DEVICE_DEFAULT_DARK, this, year, month, day);
            // DatePickerDialog THEME_HOLO_LIGHT
            android.app.DatePickerDialog dpd3 = new android.app.DatePickerDialog(getActivity(),
                    AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);
            dpd.getDatePicker().getTouchables().get(0).performClick();
            // DatePickerDialog THEME_HOLO_DARK
            android.app.DatePickerDialog dpd4 = new android.app.DatePickerDialog(getActivity(),
                    AlertDialog.THEME_HOLO_DARK, this, year, month, day);
            // DatePickerDialog THEME_TRADITIONAL
            android.app.DatePickerDialog dpd5 = new android.app.DatePickerDialog(getActivity(),
                    AlertDialog.THEME_TRADITIONAL, this, year, month, day);
//            TextView tv = new TextView(getActivity());
//
//            // Create a TextView programmatically
//            ViewGroup.LayoutParams lp = new RelativeLayout.LayoutParams(
//                    ViewGroup.LayoutParams.WRAP_CONTENT, // Width of TextView
//                    ViewGroup.LayoutParams.WRAP_CONTENT); // Height of TextView
//            tv.setLayoutParams(lp);
//            tv.setPadding(10, 10, 10, 10);
//            tv.setGravity(Gravity.CENTER);
//            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
//            tv.setText(getResources().getText(R.string.Date_Of_Birth));
//            tv.setTextColor(Color.parseColor("#ff0000"));
//            tv.setBackgroundColor(Color.parseColor("#FFD2DAA7"));
            // Set the newly created TextView as a custom tile of DatePickerDialog
            //dpd.setCustomTitle(tv);
            // Or you can simply set a tile for DatePickerDialog
            /*
                setTitle(CharSequence title)
                    Set the title text for this dialog's window.
            */
            dpd3.setTitle(getResources().getText(R.string.Date_Of_Birth));
            // Return the DatePickerDialog
            return dpd;
        }
        public void onDateSet(DatePicker view, int year, int month, int day){
            // Do something with the chosen date
        month = month + 1;
        Log.d ( TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year );
     //   dateDisplay = day + "/" + month + "/" + year;
      //  mDisplayDate.setText ( date );
        }
    }

在styles.xml

 <style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">@color/colorPrimary</item>
    </style>

下面是实现此用例的 DatePickerDialog 属性

dpDialog.getDatePicker().getTouchables().get(0).performClick();