android 获取Datepicker日期

1.使用的Android5.0系统,实现上面效果使用了alertdialog

2.布局文件: layout_dataselect

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <DatePicker
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:id="@+id/datepicker1"
        android:calendarViewShown="false"     //  使得控件不会出现 日历样式
        android:datePickerMode="spinner"            // 使得控件不会使用5.0新样式
        />
</LinearLayout>

  

2.主布局加一个Button,textView即可:

3.

 textView_datastart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder localBuilder = new AlertDialog.Builder(WenjianSearch_Activity.this);
                localBuilder.setTitle("选择时间").setIcon(R.mipmap.ic_launcher);
                //
                final LinearLayout layout_alert= (LinearLayout) getLayoutInflater().inflate(R.layout.layout_dataselect, null);
                localBuilder.setView(layout_alert);
                localBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
                    {
                        DatePicker datepicker1= (DatePicker) layout_alert.findViewById(R.id.datepicker1);
                        int y=datepicker1.getYear();
                        int m=datepicker1.getMonth()+1;
                        int d=datepicker1.getDayOfMonth();
                        System.out.println("y:"+y+" m:"+m+" d:"+d);
                        textView_datastart.setText(y+"-"+m+"-"+d); //  获取时间


                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
                    {

                    }
                }).create().show();
            }
        });

  

今天多一点积累,明天少一分烦恼
原文地址:https://www.cnblogs.com/galibujianbusana/p/6184425.html