在开发中,可以通过DatePickerDialog来设置日期,TimePickerDialog来设置时间。

实例化DatePickerDialog对象之后,再调用show方法就可以显示对话框了。

具体的api如下所示:

public DatePickerDialog(Context context,

                        DatePickerDialog.OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth)
参数:
context - The context the dialog is to run in.(上下文)
- How the parent is notified that the date is set.(日期设置的监听器)
year - The initial year of the dialog.(初始的年份)
monthOfYear - The initial month of the dialog.(初始的月份)
dayOfMonth - The initial day of the dialog.(初始的日期)
注意:如果使用Calendar类的Calendar.MONTH来初始化月份monthOfYear,在获取月份时记得加1.
因为Calendar.MONTH的月份是从0开始的,0表示1月份,1表示2月份。 具体的代码如下所示: MainActivity.java
package com.example.datepickerdialogdemo;

import java.util.Calendar;
import java.util.Date; import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker; public class MainActivity extends Activity {
Button datePicker;
Button timePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker=(Button)findViewById(R.id.date_picker);
timePicker=(Button)findViewById(R.id.time_picker);
datePicker.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar calendar=Calendar.getInstance();
DatePickerDialog dpd=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override
//监听日期的变化.
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//由于使用Calendar.MONTH初始化月份,而Calendar.MONTH是从0开始的,因此在获取月份时,记得加1.
EditText date = (EditText) findViewById(R.id.date_text);
date.setText( year + "-" + (monthOfYear+1)
+ "-" + dayOfMonth);
}
} , calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dpd.show(); }
});
timePicker.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//时间设置的对话框和日期设置的对话框的构造方法非常类似。以下直接new对象调用show方法.省去不必要的对象命名
Calendar calendar=Calendar.getInstance();
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override
public void onTimeSet(TimePicker picker, int hourOfDay, int minute) {
// TODO Auto-generated method stub
EditText time = (EditText) findViewById(R.id.time_text);
time.setText(hourOfDay+":"+minute);
}
}, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

activity_main.xml
<LinearLayout 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"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DatePicker"
android:id="@+id/date_picker"/> <EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="select date"
android:id="@+id/date_text"
/>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/TimePicker"
android:id="@+id/time_picker" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="select time"
android:id="@+id/time_text"
/>
</LinearLayout>
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">DatePickerDialogDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="DatePicker">日期</string>
<string name="TimePicker">时间</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.datepickerdialogdemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.datepickerdialogdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
运行效果如下所示:

android笔记:DatePickerDialog日期设置对话框的更多相关文章

  1. android 开发DatePickerDialog/TimePickerDialog对话框的实现

    AndroidAPI提供了Dialog对话框控件,DatePickerDialog/TimePickerDialog均是AlertDialog的子类,通过DatePickerDialog/TimePi ...

  2. Android笔记之自定义对话框

    效果如下图 对话框布局 dialog_uninstallation_confirmation.xml <?xml version="1.0" encoding="u ...

  3. Android笔记之为自定义对话框添加移动动画效果

    给底部的对话框添加移动动画效果 可通过Window.setWindowAnimations(int resId)设置 SharingDialog.java package com.bu_ish.sha ...

  4. Android课程---时间日期对话框

    activity_ui2.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  5. Android开发学习之对话框浅析

    对话框式程序运行中弹出的窗口.Android系统中有四种默认的对话框:警告对话框AlertDialog.进度对话框ProgressDialog.日期选择对话框DatePickerDialog以及时间选 ...

  6. Android笔记:调试android程序

    1.Debug 第一步: 添加断点 第二步: 右击项目→Debug As→Android Application  //之后一个对话框出现,一会自动消失 第三步: 执行手机端操作,Eclipse 就会 ...

  7. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  8. android linux shell 日期设置

    /************************************************************************ android linux shell 日期设置 * ...

  9. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

随机推荐

  1. Ubuntu 系统密码相关问题

    第一个问题: Ubuntu 密码失效解决办法 拷贝:http://www.myexception.cn/operating-system/1707766.html ubuntu14.04突然不能登录, ...

  2. SQLServer2012中用于记录数据操作时刻的附加字段使用datetime2(3)就可以了

    datetime2(3)精确到毫秒(听说),约等于2005时代的datetime类型.实际上后者是精确到3.33毫秒(也是听说). ) = GETDATE(); ) = GETDATE(); ) = ...

  3. How to Failover the ‘Cluster Group’

    If you have more than two nodes in the cluster, you can specify the destination node with the follow ...

  4. Tcpdump使用常用9实例

    以下将给出9个使用tcpdump的例子,以说明tcpdump的具体使用方法. 1.针对特定网口抓包(-i选项) 当我们不加任何选项执行tcpdump时,tcpdump将抓取通过所有网口的包:使用-i选 ...

  5. ElasticSearch安装及部署

    安装及部署 一.环境配置 操作系统:Cent OS 7ElasticSearch版本:1.3.2JDK版本:1.7.0_51SSH Secure Shell版本:XShell 5elasticsear ...

  6. 正常月报表年初未分配利润修改backup

    原来:GLQC('3132',K('年')-1,'12',,,'本币','N','','本币','0001')+GLQC('314115',K('年')-1,'01',,,'本币','N','','本 ...

  7. Junit4常用注解

    Junit4注解 JUnit4的测试类不用再继承TestCase类了.使用注解会方便很多. @Before:初始化方法@After:释放资源@Test:测试方法,在这里可以测试期望异常和超时时间@Ig ...

  8. 如何把项目部署到OSChina上

    1. 在苹果电脑终端   ls -la  查看当前目录所有的隐藏文件 2. 删除 .ssh文件  rm -rf .ssh 3.创建一个隐藏的文件  mkdir .ssh   在查看 4.进入 .ssh ...

  9. ant 自定义taskdef的工作目录

    上次同事在用ant执行多层目录的测试用例的时候遇到了一些问题,也就是自定义的taskdef的工作目录总是在开始执行ant的那个目录,而有一些地方用到了当前目录,因此很多测试用命的代码出现了“找不到自定 ...

  10. PHP判断访问终端,电脑或手机访问

    函数代码: //判断电脑或手机访问 function is_mobile(){ $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_agents = ...