DatePickerDialog not working as expected

限于喜欢 提交于 2019-12-23 02:52:41

问题


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>


来源:https://stackoverflow.com/questions/42801633/datepickerdialog-not-working-as-expected

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