首页 > 系统 > Android > 正文

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

2019-12-12 00:17:01
字体:
来源:转载
供稿:网友

本文实例讲述了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:这里再为大家推荐几款关于日期与时间计算的在线工具供大家参考使用:

在线日期/天数计算器:
http://tools.VeVB.COm/jisuanqi/date_jisuanqi

在线万年历日历:
http://tools.VeVB.COm/bianmin/wannianli

在线阴历/阳历转换工具:
http://tools.VeVB.COm/bianmin/yinli2yangli

Unix时间戳(timestamp)转换工具:
http://tools.VeVB.COm/code/unixtime

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表