【Android】時間選擇器,選擇日期DatePicker 簡單詳解demo及教程

做者:程序員小冰,GitHub主頁:https://github.com/QQ986945193
新浪微博:http://weibo.com/mcxiaobing
首先給你們看一下咱們今天這個最終實現的效果圖:
這裏寫圖片描述
這個比較簡單,具體效果UI圖以及時間顯示樣式,你們能夠本身修改。
整體來講,這個效果實現起來仍是比較簡單的,我相信你可以移植到本身的項目中。
佈局比較簡單,就有一個button以及textview。因此我就先放java實現代碼吧:java

package daviddatepickerdemo.qq986945193.com.daviddatepickerdemo;


import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
/** * @author :程序員小冰 * @新浪微博 :http://weibo.com/mcxiaobing * @GitHub: https://github.com/QQ986945193 * @CSDN博客: http://blog.csdn.net/qq_21376985 * @碼雲OsChina :http://git.oschina.net/MCXIAOBING */

/** * 時間選擇器 */
public class MainActivity extends Activity {
    int mYear, mMonth, mDay;
    Button btn;
    TextView dateDisplay;
    final int DATE_DIALOG = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.dateChoose);
        dateDisplay = (TextView) findViewById(R.id.dateDisplay);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG);
            }
        });

        final Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG:
                return new DatePickerDialog(this, mdateListener, mYear, mMonth, mDay);
        }
        return null;
    }

    /** * 設置日期 利用StringBuffer追加 */
    public void display() {
        dateDisplay.setText(new StringBuffer().append(mMonth + 1).append("-").append(mDay).append("-").append(mYear).append(" "));
    }

    private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            display();
        }
    };
}

xml佈局文件代碼以下:android

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical">

    <TextView  android:id="@+id/dateDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是默認時間顯示"/>

    <Button  android:id="@+id/dateChoose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選擇日期" />
</LinearLayout>

好了,教程到此結束。若是此文章幫到了你,歡迎點贊。
源代碼須要的能夠去
(AndroidStudio版)github下載地址:
https://github.com/QQ986945193/DavidDatePickerDemogit

相關文章
相關標籤/搜索