按下按钮时在活动中创建日期选择器

时间:2015-03-16 14:48:06

标签: android datepicker android-spinner

当我点击活动中的按钮时,我正在尝试创建一个日期选择器。但它在运行时中显示日历。我想在XML中获得与(android:datePickerMode =“spinner”)相同的结果。我尝试了以下方法,但它不起作用。

private DatePicker createNewDatePicker( ) {
    layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    newDatePicker= new DatePicker(this);
    newDatePicker.setSpinnersShown(true);
    newDatePicker.setCalendarViewShown(false);
    newDatePicker.setLayoutParams(layoutParams);
    return newDatePicker;
}

有人可以建议一个解决方案吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

datePickerDialog = new DatePickerDialog(this, AddIProfile.this,

calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 

calendar.get(Calendar.DAY_OF_MONTH));


findViewById(R.id.dateButton).setOnClickListener(new View.OnClickListener() {

 @Override
            public void onClick(View v) {

                datePickerDialog.show();

            }

        });

@Override

    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {

        calendar.set(Calendar.YEAR, year);

        calendar.set(Calendar.MONTH, month);

        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        date = calendar.getTime();


    }