一个好用的日期选择器

效果图:

一个好用的日期选择器_第1张图片

添加依赖:

compile 'cn.aigestudio.datepicker:DatePicker:2.2.0'

首先是xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bruce.testbufferknife.TestTimeActivity">

    <cn.aigestudio.datepicker.views.DatePicker
        android:id="@+id/DataPicker"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

然后是java:

package com.bruce.testbufferknife;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.aigestudio.datepicker.cons.DPMode;
import cn.aigestudio.datepicker.views.DatePicker;

public class TestTimeActivity extends Activity {

    @Bind(R.id.DataPicker)
    DatePicker DataPicker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_time);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        DatePicker picker = (DatePicker) findViewById(R.id.DataPicker);
        Calendar a = Calendar.getInstance();
        int year = a.get(Calendar.YEAR);// 年
        int month = a.get(Calendar.MONTH) + 1;//由于月份是从0开始的所以加1
        int day = a.get(Calendar.DATE);//日
        Log.w("year", "" + year);
        Log.w("month", "" + month);
        picker.setDate(year, month);
        picker.setMode(DPMode.SINGLE);
        picker.setOnDatePickedListener(new DatePicker.OnDatePickedListener() {
            @Override
            public void onDatePicked(String date) {
                Toast.makeText(TestTimeActivity.this, date, Toast.LENGTH_LONG).show();
            }
        });
    }
}
github地址: https://github.com/AigeStudio/DatePicker

你可能感兴趣的:(一个好用的日期选择器)