DatePickerDialog的使用
2016-05-16 15:16
543 查看
Android应用中,日期控件有DatePicker和DatePickerDialog,二者作用基本一样。DatePickerDialog的使用要稍微复杂一点,它是以弹出式对话框形式出现的,并需要实现 OnDateSetListener 接口(主要是 onDateSet 方法)。
1、Layout
< RelativeLayout
xmlns:Android = "http://schemas.android.com/apk/res/android"
android:background = "@drawable/top"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
>
< TextView android:layout_width = "wrap_content"
android:layout_height = "wrap_content" android:text = " 查询日期: "
android:textSize = "18sp" />
< TextView android:id = "@+scanbycol/tvDate"
android:layout_width = "wrap_content" android:layout_height = "wrap_content"
android:layout_alignParentTop = "true" />
< Button android:id = "@+scanbycol/btnDatePicker" android:text = " 编辑 "
android:layout_toRightOf = "@scanbycol/tvDate" android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
</ RelativeLayout >
我们在xml中,加入了两个textview和一个button。第2个textview显示日期,button用于弹出DatePickerDialog,当用户在DatePickerDialog中修改了日期,则textview接收修改后的日期。
2、Activity
首先,实现OnDateSetListener(其中的onDateSet方法):
// 日期选择对话框的 DateSet 事件监听器
private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){ //
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3)
{
cal .set(Calendar. YEAR , arg1);
cal .set(Calendar. MONTH , arg2);
cal .set(Calendar. DAY_OF_MONTH , arg3);
updateDate();
}
};
// 当 DatePickerDialog 关闭,更新日期显示
private void updateDate(){
df = new SimpleDateFormat( "yyyy-MM-dd" );
tvDate .setText( df .format( cal .getTime()));
}
这样,当DatePickerDialog中的日期被改变后,textview中的日期也做相应改变。
接下来,在onCreate方法中,绑定button的onClickListener:
// 选择日期按钮
btnDate =(Button)findViewById(R.scanbycol. btnDatePicker );
btnDate .setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
// 构建一个 DatePickerDialog 并显示
new DatePickerDialog(ScanByColActivity. this ,
listener ,
cal .get(Calendar. YEAR ),
cal .get(Calendar. MONTH ),
cal .get(Calendar. DAY_OF_MONTH )
).show();
}
});
这样,当点击button后,会构造一个DatePickerDialog对话框并显示给用户。
整个效果如下图:
1、Layout
< RelativeLayout
xmlns:Android = "http://schemas.android.com/apk/res/android"
android:background = "@drawable/top"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
>
< TextView android:layout_width = "wrap_content"
android:layout_height = "wrap_content" android:text = " 查询日期: "
android:textSize = "18sp" />
< TextView android:id = "@+scanbycol/tvDate"
android:layout_width = "wrap_content" android:layout_height = "wrap_content"
android:layout_alignParentTop = "true" />
< Button android:id = "@+scanbycol/btnDatePicker" android:text = " 编辑 "
android:layout_toRightOf = "@scanbycol/tvDate" android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
</ RelativeLayout >
我们在xml中,加入了两个textview和一个button。第2个textview显示日期,button用于弹出DatePickerDialog,当用户在DatePickerDialog中修改了日期,则textview接收修改后的日期。
2、Activity
首先,实现OnDateSetListener(其中的onDateSet方法):
// 日期选择对话框的 DateSet 事件监听器
private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){ //
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3)
{
cal .set(Calendar. YEAR , arg1);
cal .set(Calendar. MONTH , arg2);
cal .set(Calendar. DAY_OF_MONTH , arg3);
updateDate();
}
};
// 当 DatePickerDialog 关闭,更新日期显示
private void updateDate(){
df = new SimpleDateFormat( "yyyy-MM-dd" );
tvDate .setText( df .format( cal .getTime()));
}
这样,当DatePickerDialog中的日期被改变后,textview中的日期也做相应改变。
接下来,在onCreate方法中,绑定button的onClickListener:
// 选择日期按钮
btnDate =(Button)findViewById(R.scanbycol. btnDatePicker );
btnDate .setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
// 构建一个 DatePickerDialog 并显示
new DatePickerDialog(ScanByColActivity. this ,
listener ,
cal .get(Calendar. YEAR ),
cal .get(Calendar. MONTH ),
cal .get(Calendar. DAY_OF_MONTH )
).show();
}
});
这样,当点击button后,会构造一个DatePickerDialog对话框并显示给用户。
整个效果如下图:
![](http://hi.csdn.net/attachment/201104/13/0_130267644926z6.gif)
相关文章推荐
- VS2015 中代码移动快捷键
- Exchange 2013诡异问题解决过程分享
- CSS3:背景
- cotex_m3内核提供的ITM串口打印调试
- allowDefinition='MachineToApplication错误
- LR中忽略Socket接收数据长度验证
- DrawCall相关优化(Unity 5.4)
- Linux的sed和awk
- C++的XML编程经验――LIBXML2库使用指南
- Android从按下开机键到启动发生了什么
- ACE框架整合记录1基础框架【完结】
- iOS开发:环信实现即时通讯
- 数据时代技能书单
- 使用Entity Framework 自动产生的Sql语句
- Spring(二)——Spring管理Bean的原理
- android原生与js的交互。
- Recording HTTP traffic on iOS devices with HP LoadRunner
- linux软件安装
- VNC Viewer 设置屏幕分辨率-解决屏幕分辨率问题
- EM(Expectation-Maximization)算法的浅显理解