android 自带日历控件datePicker

//这就是一个点击事件,点击打开日历拾取器
selectDate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view1) {
        AlertDialog.Builder builder = new AlertDialog.Builder(BooksActivity.this);

        View view = getLayoutInflater().inflate(R.layout.activity_dialog_date, null);//这个布局在下边,可参考
        final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
        //设置日期简略显示 否则详细显示 包括:星期周
        datePicker.setCalendarViewShown(false);

        //初始化当前日期
        calendar.setTimeInMillis(System.currentTimeMillis());
        //初始化当前日期
        calendar.setTimeInMillis(System.currentTimeMillis());
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), null);
        /**
         * 下面这行代吗 设置的是只显示年月
         */
        ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);


        //设置date布局
        builder.setView(view);
        builder.setTitle("设置日期信息");


        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                int year = datePicker.getYear();
                int month = datePicker.getMonth()+1;//我勒个去,系统获取的日期居然不准 

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.cancel();
            }
        });
        dialog = builder.create();
        dialog.show();

    }
});
//布局文件
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_coupon"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:background="@color/color_retangle"
    >

                    android:id="@+id/date_picker"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:datePickerMode="spinner"
            >
        

你可能感兴趣的:(android 自带日历控件datePicker)