当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发之DatePickerDialog、TimePickerDialog时间日期对话框用法示例

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

2019年07月23日  | 移动技术网IT编程  | 我要评论

av小游戏,摩托罗拉防爆对讲机,闺蜜乱了电视剧

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

用法:

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

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

效果:

datepickerdialog

timepickerdialog

下面是具体的实现方法:

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();
   }
  });
 }
}

这里是布局文件:

<?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>

ps:这里再为大家推荐几款关于日期与时间计算的在线工具供大家参考使用:

在线日期/天数计算器:

在线万年历日历:

在线阴历/阳历转换工具:

unix时间戳(timestamp)转换工具:

更多关于android相关内容感兴趣的读者可查看本站专题:《android日期与时间操作技巧总结》、《android开发入门与进阶教程》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网