简体   繁体   English

datepickerdialog隐藏日历并将我的日期选择器视图更改为标准模式

[英]datepickerdialog hide calendar and change my date picker view to standard mode

I want to change my date picker view to standard mode 我想将日期选择器视图更改为标准模式

From

在此输入图像描述

To

在此输入图像描述

My code is 我的代码是

dateOfBirthET = (EditText) findViewById(R.id.dateOfBirth);
        //setting dateSetListener
        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();
                updateLabelToSave();
            }

        };

        //setting onClickListener on setDate
        dateOfBirthET.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                DatePickerDialog dpd = new DatePickerDialog(RegisterActivity.this, date,
                        myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH));


                //setting maxDate on tempCal
                long maxDate = new Date().getTime();
                tempCal.setTimeInMillis(maxDate);
                tempCal.set(Calendar.YEAR, tempCal.get(Calendar.YEAR) - 16);

                dpd.getDatePicker().setMaxDate(tempCal.getTimeInMillis());

                dpd.show();
            }
        });

    }

I am tried this code also but not working 我也尝试了这段代码,但没有工作

dpd.getDatePicker().setCalendarViewShown(false);

You just need to change theme you want while creating DatePickerDialog instance 您只需在创建DatePickerDialog实例时更改所需的主题

DatePickerDialog dpd = new DatePickerDialog(RegisterActivity.this, android.R.style.Theme_Holo_Dialog ,date,
                        myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH));

Working for me tested android 6.0 marshmallow. 为我工作测试了android 6.0 marshmallow。

From the setCalendarViewShown doc it says 它来自setCalendarViewShown doc

Calling this method has no effect when the DatePicker_datePickerMode attribute is set to calendar. 将DatePicker_datePickerMode属性设置为calendar时,调用此方法无效。

And as of Android L, with the Material theme selected, the default layout of android:datePickerMode is calendar (see here ) 从Android L开始,选择了Material主题, android:datePickerMode的默认布局是日历(参见这里

So if you want to change your DatePickerDialog to spiner style, you must change android:datePickerMode 因此,如果要将DatePickerDialog更改为spiner样式,则必须更改android:datePickerMode

And now I see datePickerMode can not change programmatically, it can only change by config XML like 现在我看到datePickerMode无法以编程方式进行更改,它只能通过config XML来改变

<DatePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:datePickerMode="spinner"
    />

Therefore I think you should create a custom your DatePickerDilog layout 因此,我认为您应该创建一个自定义DatePickerDilog布局

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM