Java 日期选择器不显示

Java 日期选择器不显示,java,android,datepicker,Java,Android,Datepicker,我试图从一个按钮创建一个弹出日期选择器(弹出它的按钮也显示日期)。但是弹出窗口没有显示。按下按钮时,什么也没发生 DatePicker需要从xml中强制转换,因为我希望它显示为微调器而不是日历(根据API 21之后的内容,唯一的方法是使用xml) 下面是我为按钮的onClickListner编写的代码: final Button startDate = (Button)findViewById(R.id.startDate); Calendar currentDate = Calendar.ge

我试图从一个按钮创建一个弹出日期选择器(弹出它的按钮也显示日期)。但是弹出窗口没有显示。按下按钮时,什么也没发生

DatePicker需要从xml中强制转换,因为我希望它显示为微调器而不是日历(根据API 21之后的内容,唯一的方法是使用xml)

下面是我为按钮的onClickListner编写的代码:

final Button startDate = (Button)findViewById(R.id.startDate);
Calendar currentDate = Calendar.getInstance();
mYear = currentDate.get(Calendar.YEAR);
mMonth = currentDate.get(Calendar.MONTH);
int mDay = currentDate.get(Calendar.DAY_OF_MONTH);

startDate.setText(mMonth + "/" + mDay + "/" + mYear);

startDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //get date from button text
            Calendar dateInButton = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            try {
                dateInButton.setTime(sdf.parse(startDate.getText().toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int pYear = dateInButton.get(Calendar.YEAR);
            int pMonth = dateInButton.get(Calendar.MONTH);
            int pDay = dateInButton.get(Calendar.DAY_OF_MONTH);

            //display popup datepicker
            final View dialogView = View.inflate(MyActivity.this, R.layout.date_picker, null);

            DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
            datePicker.updateDate(pYear, pMonth, pDay);
            datePicker.init(pYear, pMonth, pDay, new DatePicker.OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker view, int year, int month, int day) {
                    month++;
                    startDate.setText(month + "/" + day + "/" + year);
                }
            });

        }
    });

最好使用
DatePicker对话框
thenYes,从API 21,您只能看到DatePicker的日历视图,如果您想要像以前版本的API一样查看,您需要为其创建自定义xml布局,并将其添加到警报对话框中。将其添加到警报对话框中是可行的。谢谢最好使用
DatePicker对话框
thenYes,从API 21,您只能看到DatePicker的日历视图,如果您想要像以前版本的API一样查看,您需要为其创建自定义xml布局,并将其添加到警报对话框中。将其添加到警报对话框中是可行的。谢谢