一手遮天 Android - view(弹出类): DatePickerDialog 基础

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(弹出类): DatePickerDialog 基础

示例如下:

/view/flyout/DatePickerDialogDemo1.java

/**
 * DatePickerDialog - 日期选择对话框
 *
 * 注1:DatePickerDialog 继承自 AlertDialog
 * 注2:实际项目中建议使用第三方的日期选择控件
 */

package com.webabcd.androiddemo.view.flyout;

import android.app.DatePickerDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

import com.webabcd.androiddemo.R;

import java.util.Calendar;

public class DatePickerDialogDemo1 extends AppCompatActivity {

    private Button mButton1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_flyout_datepickerdialogdemo1);

        mButton1 = findViewById(R.id.button1);

        sample();
    }

    private void sample() {
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                DatePickerDialog datePickerDialog = new DatePickerDialog(DatePickerDialogDemo1.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 获取用户选择的日期(注:月份要加上 1)
                        Toast.makeText(DatePickerDialogDemo1.this, String.format("%d-%d-%d", year, monthOfYear + 1, dayOfMonth), Toast.LENGTH_SHORT).show();
                    }
                }
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DAY_OF_MONTH));

                datePickerDialog.show();
            }
        });
    }
}

/layout/activity_view_flyout_datepickerdialogdemo1.xml

<?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"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="弹出日期选择对话框" />

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 13:19  webabcd  阅读(50)  评论(0编辑  收藏  举报