-->

DatePickerDialog not working as expected

2019-06-04 05:18发布

问题:

I have an EditView in which I am trying to bind DatePickerDialog. Its working well with the api 19. but when tested on api version 25 Marshmallow the calendar becomes white and when selected some date the EditView is not populated as well i.e I am unable to set date in Marshmallow.

Code:

warrantyExpEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(getContext(), date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });


    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }

        };
        private void updateLabel() {

            String myFormat = "yyyy-MM-dd";
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

            warrantyExpEt.setText(sdf.format(myCalendar.getTime()));
        }

回答1:

this might be the Theme you are using in your project. So use below theme in your style file

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="colorPrimary">@color/green_sheet</item>
   <item name="android:textAllCaps">false</item>     
</style>

Hope this will help you out.



回答2:

Remove this from your style:

<item name="android:textColorPrimary">@color/white</item>


回答3:

try to this it can show dialog as you want

   new DatePickerDialog(MainActivity.this, R.style.datepicker, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {

            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, day);

        }
    }, year, month, day).show();

dialog style as you wish

  <style name="datepicker" parent="Theme.AppCompat.Light.Dialog">

    <item name="colorAccent">@color/red</item>
</style>