服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android开发之DatePickerDialog、TimePickerDialog时间日期对话框用法示例

Android开发之DatePickerDialog、TimePickerDialog时间日期对话框用法示例

2022-09-29 17:11水中鱼之1999 Android

这篇文章主要介绍了Android开发之DatePickerDialog、TimePickerDialog时间日期对话框用法,结合实例形式分析了Android使用DatePickerDialog、TimePickerDialog显示日期时间相关操作技巧,需要的朋友可以参考下

本文实例讲述了android开发之datepickerdialog、timepickerdialog时间日期对话框用法。分享给大家供大家参考,具体如下:

用法:

一、创建两个 datepickerdialog、timepickerdialog 实例调用 show() 方法即可将他们显示出来

二、为 datepickerdialog、timepickerdialog 实例分别绑定监听器,通过监听获得用户设置

效果:

datepickerdialog

Android开发之DatePickerDialog、TimePickerDialog时间日期对话框用法示例

timepickerdialog

Android开发之DatePickerDialog、TimePickerDialog时间日期对话框用法示例

下面是具体的实现方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class mainactivity extends activity {
 private button buttondate;
 private button buttontime;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  buttondate = (button) findviewbyid(r.id.databn);
  buttontime = (button) findviewbyid(r.id.timebn);
  iniclick();//binding the listeners for you program
 }
 public void iniclick(){
  //set listener for your date button
  buttondate.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    calendar calendar = calendar.getinstance();
    //create a datepickerdialog and then shoe it on your screen
    new datepickerdialog(mainactivity.this,//binding the listener for your datepickerdialog
      new datepickerdialog.ondatesetlistener() {
       @override
       public void ondateset(datepicker view, int year, int month, int dayofmonth) {
        toast.maketext(mainactivity.this,"year:" + year + " month:" + month + " day:" + dayofmonth,toast.length_short).show();
       }
      }
      , calendar.get(calendar.year)
      , calendar.get(calendar.month)
      , calendar.get(calendar.day_of_month)).show();
   }
  });
  //set listener for your time button
  buttontime.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    calendar calendar = calendar.getinstance();
    //create a datepickerdialog and then shoe it on your screen
    new timepickerdialog(mainactivity.this,
      new timepickerdialog.ontimesetlistener() {
       @override
       public void ontimeset(timepicker view, int hourofday, int minute) {
        toast.maketext(mainactivity.this,"hour:" + hourofday + " minute:" + minute ,toast.length_short).show();
       }
      }
      , calendar.get(calendar.hour_of_day)
      , calendar.get(calendar.minute)
      , true).show();
   }
  });
 }
}

这里是布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8" ?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/idtatabhost"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal"
 android:layout_weight="1">
 <button
  android:id="@+id/databn"
  android:text="点我一下 挑日期"
  android:layout_width="0dp"
  android:layout_weight="1"
  android:layout_height="wrap_content" />
 <button
  android:id="@+id/timebn"
  android:text="点我一下 挑时间 。。。"
  android:layout_width="0dp"
  android:layout_weight="1"
  android:layout_height="wrap_content" />
</linearlayout>

希望本文所述对大家android程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_43377749/article/details/85038563

延伸 · 阅读

精彩推荐