android笔记:DatePickerDialog日期设置对话框
在开发中,可以通过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日期设置对话框的更多相关文章
- android 开发DatePickerDialog/TimePickerDialog对话框的实现
AndroidAPI提供了Dialog对话框控件,DatePickerDialog/TimePickerDialog均是AlertDialog的子类,通过DatePickerDialog/TimePi ...
- Android笔记之自定义对话框
效果如下图 对话框布局 dialog_uninstallation_confirmation.xml <?xml version="1.0" encoding="u ...
- Android笔记之为自定义对话框添加移动动画效果
给底部的对话框添加移动动画效果 可通过Window.setWindowAnimations(int resId)设置 SharingDialog.java package com.bu_ish.sha ...
- Android课程---时间日期对话框
activity_ui2.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- Android开发学习之对话框浅析
对话框式程序运行中弹出的窗口.Android系统中有四种默认的对话框:警告对话框AlertDialog.进度对话框ProgressDialog.日期选择对话框DatePickerDialog以及时间选 ...
- Android笔记:调试android程序
1.Debug 第一步: 添加断点 第二步: 右击项目→Debug As→Android Application //之后一个对话框出现,一会自动消失 第三步: 执行手机端操作,Eclipse 就会 ...
- Android笔记——Android中数据的存储方式(二)
我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...
- android linux shell 日期设置
/************************************************************************ android linux shell 日期设置 * ...
- Android笔记:触摸事件的分析与总结----TouchEvent处理机制
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320 ...
随机推荐
- [Spring MVC] - InitBinder验证
Spring MVC使用InitBinder验证: 使用InitBinder做验证的情况一般会在此Controller中提交的数据需要有一些是业务性质的,也即比较复杂的验证情况下才会使用.大部份简单的 ...
- 一加3,CM13蓝牙共享互联网 无效。
一加3准备把4G网络共享给魅族PRO5. 但在一加3的蓝牙设置里怎么勾选都无用. 最后发现在要在PRO5上设置才行. 1.在蓝牙列表中,点击带圈的感叹号. 2.选择“互联网访问”. - --
- 用JavaScript动态加载CSS和JS文件
本文转载自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/11/14/2248451.html 今天项目中需要用到动态加载 CSS 文件 ...
- BP(back propagation)反向传播
转自:http://www.zhihu.com/question/27239198/answer/89853077 机器学习可以看做是数理统计的一个应用,在数理统计中一个常见的任务就是拟合,也就是给定 ...
- 堆外内存操作类ByteBuffer
本篇主要讲解如何使用直接内存(堆外内存),并按照下面的步骤进行说明: 1 相关背景-->读写操作-->关键属性-->读写实践-->扩展-->参考说明 希望对想使用直接内存 ...
- linux下开启mysql慢查询,分析查询语句
一,为什么要开启这个查询呢? 数据库是很容易产生瓶颈的地方,现在Nosql大家讨论这么热,估计都被数据库搞郁闷了.mysql中最影响速度的就是那些查询非常慢的语句,这些慢的语句,可能是写的不够合理或者 ...
- Linux中cp和scp命令的使用方法
Linux为我们提供了两个用于文件copy的命令,一个是cp,一个是scp,但是他们略有不同. cp --- 主要是用于在同一台电脑上,在不同的目录之间来回copy文件 scp --- 主要是在不同的 ...
- iOS Storyboard全解析
来源:http://iaiai.iteye.com/blog/1493956 Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果, ...
- 实习日记:图像检索算法 LSH 的总结与分析
先贴上这两天刚出炉的C++代码.(利用 STL 偷了不少功夫,代码待优化) Head.h #ifndef HEAD_H #define HEAD_H #include "D:\\LiYang ...
- QTP实现功能测试的时候,当新版本的页面都改变了,应该如何解决
去更改对象仓库的属性和更改对象仓库.