使用DatePickerDialog、TimePickerDialog
DatePickerDialog与TimerPicker的功能比较简单,用户也简单,只要如下两步即可。
①通过new关键字创建DatePickerDialog、TimePickerDialog实例,调用它们的show()方法即可将日期选择对话框、时间选择对话框显示出来。
②为DatePickerDialog、TimePickerDialog绑定监听器,这样可以保证用户通过DatePickerDialog、TimePickerDialog设置事件时触发监听器,从而通过监听器来获取用户设置的事件。
下面的程序中定义了两个按钮,一个按钮用于打开日期选择对话框,一个用于打开时间对话框。该程序的界面布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<EditText android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button android:id="@+id/dateBn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置日期"/>
<Button android:id="@+id/timeBn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置时间"/> </LinearLayout>
</LinearLayout>
对应的Activity后台代码文件如下:
package org.crazyit.helloworld; import java.util.Calendar; 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 DateDialogTest extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_dialog_test);
Button dateBn=(Button)findViewById(R.id.dateBn);
Button timeBn=(Button)findViewById(R.id.timeBn);
//为“设置日期”按钮绑定监听器
dateBn.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Calendar c=Calendar.getInstance();
//直接创建一个DatePickerDialog对话框实例,并将它显示出来
new DatePickerDialog(DateDialogTest.this,
//绑定监听器
new DatePickerDialog.OnDateSetListener() { @Override
public void onDateSet(DatePicker view, int year, int month,
int dayOfMonth) {
// TODO Auto-generated method stub
EditText show=(EditText)findViewById(R.id.show);
show.setText("您选择啦:"+year+"年"+(month+1)+"月"+dayOfMonth+"日");
}
},c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
} });
//为“设置时间”按钮绑定监听器
timeBn.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar c=Calendar.getInstance();
//创建一个TimePickerDialog实例,并把它显示出来
new TimePickerDialog(DateDialogTest.this,
//绑定监听器
new TimePickerDialog.OnTimeSetListener() { @Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
EditText show=(EditText)findViewById(R.id.show);
show.setText("您选择了:"+hourOfDay+"时"+minute+"分");
}
}
//设置初始时间
,c.get(Calendar.HOUR_OF_DAY)
,c.get(Calendar.MINUTE)
//true表示采用24小时制
,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.date_dialog_test, menu);
return true;
} }
上面的程序中两段粗体字代码就是创建并显示DatePickerDialog、TimePickerDialog的关键代码。运行上面的程序将会看到如下界面效果。
使用DatePickerDialog、TimePickerDialog的更多相关文章
- android 开发DatePickerDialog/TimePickerDialog对话框的实现
AndroidAPI提供了Dialog对话框控件,DatePickerDialog/TimePickerDialog均是AlertDialog的子类,通过DatePickerDialog/TimePi ...
- Android DatePickerDialog TimepickerDialog
package com.example.myact5; import java.util.Calendar; import android.app.DatePickerDialog; import a ...
- 2.5.5 使用DatePickerDialog, TimePickerDialog
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- DatePickerDialog TimePickerDialog
MainActivity.java public class MainActivity extends Activity { @Override public ...
- android入门 — ProgressDialog/DatePickerDialog/TimePickerDialog
这三个Dialog都是AlertDialog的子类. ①DatePickerDialog 1.创建DatePickerDialog的实例: 2.通过Calendar类获得系统时间: 3.通过DateP ...
- android中提示&对话框----ProgressDialog&DatePickerDialog &TimePickerDialog&PopupWindow
ProgressDialog(精度条对话框): 1.直接调用ProgressDialog提供的静态方法show()显示 2.创建ProgressDialog,再设置对话框的参数,最后show()出来 ...
- android内部培训视频_第三节(3)_常用控件(ViewPager、日期时间相关、ListView)
第三节(2):常用控件之ViewPager.日期时间相关.ListView 一.ViewPager 实例:结合PagerAdapter滑动切换图片 二.日期时间相关:AnalogClock\Dig ...
- 第32讲 UI组件之 时间日期控件DatePicker和TimePicker
第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置, Time ...
- android学习笔记19——对话框(DatePickerDialog、TimePickerDialog)
DatePickerDialog.TimePickerDialog ==> DatePickerDialog.TimePickerDialog功能.用法都比较简单,操作步骤: 1.通过new关键 ...
- 完全参照系统自带的DatePickerDialog、TimePickerDialog的源代码仿写的DateTimePickerDialog
完全参照系统自带的DatePickerDialog.TimePickerDialog的源代码仿写的DateTimePickerDialog.具有同时选择日期.时间的功能.在2.2.2.3平台,显示的效 ...
随机推荐
- UML用例图说明
转自:http://www.360doc.com/content/10/1206/23/3123_75672033.shtml 前些时间参加了潘加宇老师的技术讲座,UML建模技术受益匪浅.我也把平时的 ...
- HDU 5176 The Experience of Love 带权并查集
The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- (中等) CF 585D Lizard Era: Beginning,中途相遇。
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...
- 【HighCharts系列教程】五、版权属性——Credits
一.Credits属性说明 严格来讲,Credits并不算版权信息,官方的说法是:Highchart by default putsa credits label in the lower right ...
- springMVC入门配置及helloworld实例
1. 新建web project 2. 往lib里copy必须的jar 3. 改写web.xml <?xml version="1.0" encoding="UTF ...
- POJ 2686 Traveling by Stagecoach
状压DP dp[s][p]用了哪几张票,到哪个节点的最小费用. 注意:G++ %.3lf输出会WA,但C++能过:改成%.3f,C++,G++都能AC #include<cstdio> # ...
- java工程师联通XX面试题目
什么是“长连接”和“短连接”? 所谓短连接指建立SOCKET连接后发送后接收完数据后马上断开连接,一般银行都使用短连接解释2长连接就是指在基于tcp的通讯中,一直保持连接,不管当前是否发送或者接收数据 ...
- VC#2010 视图设计器无法打开 问题的正解
继上次VC#2010中视图设计器无法打开的问题的讨论后,我感觉每次都重新安装一次安装包未免也太麻烦了,程序员的时间都灰常宝贵. 所以在这次人工智能作业的时候,找到了一个简单的途径: 打开VC#2010 ...
- window.location获取URL中各部分
博客分类: JAVASCRIPT JavaScriptASP.netSchemeASP网络协议 URL即:统一资源定位符 (Uniform Resource Locator, URL) 完整的URL ...
- Windwos下 php mysql apache 环境配置
一.下载所需软件: 1.安装Windows组件: 安装Apache的前 必须安装 VC11 vcredist_x86.exe (64位请下载vcredist_x64.exe),选择时尽量选择英文版 ...