如何在Android Dialog datepicker中将默认显示设置为年份

时间:2016-09-26 11:20:25

标签: android android-datepicker

我正在尝试弄清楚如何在Android Dialog DatePicker中设置默认显示年份。我们的场景是第一个用户必须选择年份然后月份,最后在对话框DatePicker中选择日期。对于此要求,我们需要在打开对话框时打开年度显示。

PFB图片链接。请参考右侧图像,默认情况下应该打开。

DatePicker

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

实施您的活动:

实现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");
    }
});

外部创建

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

答案 2 :(得分:0)

下面是DatePickerDialog的属性,通过它可以实现此用例

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