Android 中的 DatePicker函数

首页 / Android入门教程 / Android 中的 DatePicker函数

Android Date Picker允许您在自定义用户界面中选择由日,月和年组成的日期。为此功能,android提供了DatePicker和DatePickerDialog组件。

在本教程中,我们将通过DatePickerDialog演示日期选择器的用法, DatePickerDialog是一个包含DatePicker的简单对话框。

为了显示DatePickerDialog,您必须将DatePickerDialog id传递给 showDialog(id_of_dialog)方法。其语法如下-

showDialog(999);

调用此 showDialog 方法时,会自动调用另一个名为 onCreateDialog 的方法。因此,我们也必须重写该方法。其语法如下-

无涯教程网

@Override
protected Dialog onCreateDialog(int id) {
   //TODO Auto-generated method stub
   if (id == 999) {
      return new DatePickerDialog(this, myDateListener, year, month, day);
   }
   return null;
}

在最后一步,您必须注册DatePickerDialog侦听器并覆盖其onDateSet方法。此onDateSet方法包含更新的日期,月份和年份。其语法如下-

private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
   @Override
   public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
      //arg1=year
      //arg2=month
      //arg3=day		
   }
};
Set Date

除日期属性外,DatePicker对象也传递给此函数。您可以使用DatePicker的以下方法执行进一步的操作。

Sr.No Method & description
1

getDayOfMonth()

此方法获取每月的选定日期

2

getMonth()

此方法获取选定的月份

3

getYear()

此方法获取选定的年份

4

setMaxDate(long maxDate)

此方法设置此DatePicker支持的最大日期(自1970年1月1日00:00:00在getDefault()时区开始),以毫秒为单位。

5

setMinDate(long minDate)

此方法设置此NumberPicker支持的最小日期(以毫秒为单位),该日期自1970年1月1日00:00:00在getDefault()时区开始

6

setSpinnersShown(boolean shown)

此方法设置是否显示微调框

7

updateDate(int year,int month,int dayOfMonth)

此方法更新当前日期

8

getCalendarView()

此方法返回日历视图

9

getFirstDayOfWeek()

此方法返回一周的第一天

示例

这是一个演示DatePickerDialog类的用法的示例。它创建一个基本的Date Picker应用程序,该应用程序允许您使用DatePicker窗口小部件设置日期。

以下是修改后的主要Activity文件 src/com.example.datepicker/MainActivity.java 的内容。

package com.example.datepicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;

import android.os.Bundle;

import android.view.Menu;
import android.view.View;

import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
   private DatePicker datePicker;
   private Calendar calendar;
   private TextView dateView;
   private int year, month, day;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      dateView = (TextView) findViewById(R.id.textView3);
      calendar = Calendar.getInstance();
      year = calendar.get(Calendar.YEAR);
      
      month = calendar.get(Calendar.MONTH);
      day = calendar.get(Calendar.DAY_OF_MONTH);
      showDate(year, month+1, day);
   }

   @SuppressWarnings("deprecation")
   public void setDate(View view) {
      showDialog(999);
      Toast.makeText(getApplicationContext(), "ca", 
         Toast.LENGTH_SHORT)
      .show();
   }

   @Override
   protected Dialog onCreateDialog(int id) {
      //TODO Auto-generated method stub
      if (id == 999) {
         return new DatePickerDialog(this, 
            myDateListener, year, month, day);
      }
      return null;
   }

   private DatePickerDialog.OnDateSetListener myDateListener = new 
      DatePickerDialog.OnDateSetListener() {
      @Override
      public void onDateSet(DatePicker arg0, 
         int arg1, int arg2, int arg3) {
         //TODO Auto-generated method stub
         //arg1=year
         //arg2=month
         //arg3=day
         showDate(arg1, arg2+1, arg3);
      }
   };

   private void showDate(int year, int month, int day) {
      dateView.setText(new StringBuilder().append(day).append("/")
      .append(month).append("/").append(year));
   }
}

以下是xml res/layout/activity_main.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"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="70dp"
      android:onClick="setDate"
      android:text="@string/date_button_set" />

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="24dp"
      android:text="@string/date_label_set"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/button1"
      android:layout_marginTop="66dp"
      android:layout_toLeftOf="@+id/button1"
      android:text="@string/date_view_set"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/button1"
      android:layout_below="@+id/textView2"
      android:layout_marginTop="72dp"
      android:text="@string/date_selected"
      android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

以下是 res/values/string.xml 的内容。

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">DatePicker</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="date_label_set">Press the button to set the date</string>
   <string name="date_button_set">Set Date</string>
   <string name="date_view_set">The Date is: </string>
   <string name="date_selected"></string>
</resources>

单击运行 Eclipse Run Icon图标工具栏。 Eclipse将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在Emulator窗口下面-

Android Date Picker Tutorial

现在,您可以看到底部标签上已经设置了日期,现在,我们将通过按"设置日期"按钮通过DatePickerDialog更改日期。按下按钮后,将出现以下屏幕。

Android Date Picker Tutorial

现在设置所需的日期,并在设置日期后按"OK"按钮。该对话框将消失,并且您新设置的日期将开始显示在屏幕上。如下所示。

Android Date Picker Tutorial

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

苏杰的产品创新课 -〔苏杰〕

TensorFlow 2项目进阶实战 -〔彭靖田〕

流程型组织15讲 -〔蒋伟良〕

Redis源码剖析与实战 -〔蒋德钧〕

去无方向的信 -〔小麥〕

李智慧 · 高并发架构实战课 -〔李智慧〕

Python实战 · 从0到1搭建直播视频平台 -〔Barry〕

零基础GPT应用入门课 -〔林健(键盘)〕

结构会议力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)