`
kanwoerzi
  • 浏览: 1640459 次
文章分类
社区版块
存档分类
最新评论

时间设置对话框 DatePickerDialog的使用

 
阅读更多
时间设置对话框DatePickerDialog的使用
一、可以同过重载DatePickerDialog的setTitle来设置个性的标题
比如:
@Override
publicvoidsetTitle(CharSequencetitle){
//TODOAuto-generatedmethodstub
java.text.DateFormatdataFormat=(DateFormat
.getDateFormat(getContext()));
mCalendar=Calendar.getInstance();
//可得到“12/31/1969(Wed)”形式的日期格式
StringstrTitle=dataFormat.format(mCalendar.getTime())
+DateFormat.format("(E)",mCalendar.getTime()).toString();
super.setTitle(dataFormat.format(strTitle);
}

二、通过onDateChanged回调函数监听时间的改变
当时间改变时(编辑)以下函数回被调用
publicvoidonDateChanged(DatePickerview,intyear,intmonth,intday){
Log.i("hubin","onDateChanged");
}
三、通过updateDate()设置年月日
publicvoid<wbr style="line-height:25px"><span style="line-height:1.3em">updateDate</span><wbr style="line-height:25px"><span style="line-height:1.3em">(intyear,intmonthOfYear,intdayOfMonth)</span><wbr style="line-height:25px">设置DatePickerDialog的年,月,日<br style="line-height:25px"><span style="line-height:25px"><span style="line-height:18px">四、通过</span></span><span style="line-height:18px"><span style="color:#993300; line-height:25px">onDateChangedListener</span>来监听</span>时间设置对话框的确定按钮被按下<span style="line-height:25px"><span style="line-height:18px"><br style="line-height:25px"></span><span style="line-height:1.3em">onDateChangedListener</span></span><wbr style="line-height:25px">的onDateSet是用于都在时间设置对话框的确定按钮被按下的监听<br style="line-height:25px"><span style="line-height:1.3em"><br style="line-height:25px"></span><wbr style="line-height:25px">importjava.util.Calendar;<br style="line-height:25px"> importjava.util.Date;<br style="line-height:25px"> importjava.util.Locale;<br style="line-height:25px"><br style="line-height:25px"> importandroid.app.Activity;<br style="line-height:25px"> importandroid.app.DatePickerDialog;<br style="line-height:25px"> importandroid.app.DatePickerDialog.OnDateSetListener;<br style="line-height:25px"> importandroid.os.Bundle;<br style="line-height:25px"> importandroid.view.View;<br style="line-height:25px"> importandroid.view.View.OnClickListener;<br style="line-height:25px"> importandroid.widget.Button;<br style="line-height:25px"> importandroid.widget.DatePicker;<br style="line-height:25px"> importandroid.widget.TextView;<br style="line-height:25px"><br style="line-height:25px"> publicclassTestClockextendsActivityimplementsOnDateSetListener,OnClickListener{<br style="line-height:25px"> /**Calledwhentheactivityisfirstcreated.*/<br style="line-height:25px"> @Override<br style="line-height:25px"> publicvoidonCreate(BundlesavedInstanceState){<br style="line-height:25px"> super.onCreate(savedInstanceState);<br style="line-height:25px"> setContentView(R.layout.main);<br style="line-height:25px"> Buttonbtn=(Button)findViewById(R.id.date);<br style="line-height:25px"> btn.setOnClickListener(this);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> publicvoidonClick(Viewv){//普通按钮事件<br style="line-height:25px"> Calendard=Calendar.getInstance(Locale.CHINA);<br style="line-height:25px"> //创建一个日历引用d,通过静态方法getInstance()从指定时区Locale.CHINA获得一个日期实例<br style="line-height:25px"> DatemyDate=newDate();<br style="line-height:25px"> //创建一个Date实例<br style="line-height:25px"> d.setTime(myDate);<br style="line-height:25px"> //设置日历的时间,把一个新建Date实例myDate传入<br style="line-height:25px"> intyear=d.get(Calendar.YEAR);<br style="line-height:25px"> intmonth=d.get(Calendar.MONTH);<br style="line-height:25px"> intday=d.get(Calendar.DAY_OF_MONTH);<br style="line-height:25px"> //获得日历中的yearmonthday<br style="line-height:25px"> DatePickerDialogdlg=newDatePickerDialog(this,this,year,month,day);<br style="line-height:25px"> //新建一个DatePickerDialog构造方法中<br style="line-height:25px"> //(设备上下文,OnDateSetListener时间设置监听器,默认年,默认月,默认日)<br style="line-height:25px"> dlg.show();<br style="line-height:25px"> //让DatePickerDialog显示出来<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> publicvoidonDateSet(DatePickerview,intyear,intmonthOfYear,intdayOfMonth){<br style="line-height:25px"> //DatePickerDialog中按钮Set按下时自动调用<br style="line-height:25px"> TextViewtxt=(TextView)findViewById(R.id.text);<br style="line-height:25px"> //通过id获得TextView对象<br style="line-height:25px"> txt.setText(Integer.toString(year)+"-"+<br style="line-height:25px"> Integer.toString(monthOfYear)+"-"+<br style="line-height:25px"> Integer.toString(dayOfMonth));<br style="line-height:25px"> //设置text<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> main.xml<br style="line-height:25px"><br style="line-height:25px"> &lt;?xmlversion="1.0"encoding="utf-8"?&gt;<br style="line-height:25px"> &lt;LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"<br style="line-height:25px"> android:orientation="vertical"<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="fill_parent"<br style="line-height:25px"> &gt;<br style="line-height:25px"> &lt;TextView<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="wrap_content"<br style="line-height:25px"> android:text="@string/hello"<br style="line-height:25px"> android:id="@+id/text"<br style="line-height:25px"> /&gt;<br style="line-height:25px"><br style="line-height:25px"> &lt;Button<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="wrap_content"<br style="line-height:25px"> android:id="@+id/date"<br style="line-height:25px"> android:text="設置時間"<br style="line-height:25px"> &gt;<br style="line-height:25px"> &lt;/Button&gt;<br style="line-height:25px"> &lt;/LinearLayout&gt; </wbr></wbr></wbr></wbr></wbr>
分享到:
评论

相关推荐

    Android 对话框 Dialog使用实例讲解

    对话框 Dialog 什么是对话框 对话框是在当前的页面之上弹出的小窗口, 用于显示一些重要的提示信息, 提示用户的输入,确认...4, 时间对话框 TimePickerDialog 5, 自定义对话框 常用普通对话框 AlertDialog 1, 普通列表对

    swift-DatePickerDialog-iOS的日期选择器对话框

    DatePickerDialog - iOS的日期选择器对话框

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

    主要介绍了Android开发之DatePickerDialog、TimePickerDialog时间日期对话框用法,结合实例形式分析了Android使用DatePickerDialog、TimePickerDialog显示日期时间相关操作技巧,需要的朋友可以参考下

    Android 日期与时间对话框

    关于android开发时使用TimePickerDialog ,DatePickerDialog来实现设置时间,日期的一个源码

    使用DatePicker实现日历选择器及使用TimePicker实现时间选择器

    使用DatePicker实现日历选择器及使用TimePicker实现时间选择器;使用DatePickerDialog实现日期选择对话框及使用TimePickerDialog实现时间选择对话框。

    Android DatePickerDialog、TimePickerDialog时间日期选择用法.rar

     case TIME_DIALOG://生成时间对话框的代码  c=Calendar.getInstance();//获取日期对象  dialog=new TimePickerDialog(//创建TimePickerDialog对象  this,  new TimePickerDialog.OnTimeSetListener(){ //创建...

    简析Android多种AlertDialog对话框效果

    4.TimePickerDialog,时间选择对话框 这里主要介绍第一种,剩下的三种都是第一种的子类,所以其方法,都可以直接使用. 创建一个对话框一般需要下面几个步骤 1.创建AlertDialog.Builder对象 2.对AlertDialog....

    自定义ExpandableListView 、短信、DatePickerDialog时间控件

    自定义ExpandableListView 收缩列表 短信,对话框时间控件。 呵呵,混合型的功能。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

    Android AlertDialog对话框详解及实例

    Android AlertDialog ...4:TimePickerDialog:时间选择器对话框 创建一个对话框的步骤: AlertDialog.Builder builder = new AlertDialog.Builder(this) // 1:设置对话框标题 .setTitle(自定义列表项对话框)

    Android中几个基本的对话框

    时间对话框8.用户自定义对话框 基本对话框介绍 对话框 用法 普通 AlertDialog.Builder() 单选 AlertDialog.Builder() – setSingleChoiceItems() 多选 AlertDialog.Builder() – setMultiChoiceItems() ...

    Android下几个相关的时间日期控件的用法

    Android下几个相关的时间日期控件的用法,包括DatePicker(日期选择控件)、TimePicker(时间选择控件)、DatePickerDialog(日期选择对话框)、TimePickerDialog(时间选择对话框)、AnalogClock(模拟时钟控件)、...

    Android--UI之DatePicker、TimePicker

    包括DatePicker(日期选择控件)、TimePicker(时间选择控件)、DatePickerDialog(日期选择对话框)、TimePickerDialog(时间选择对话框)、AnalogClock(模拟时钟控件)、DigitalClock(数字时钟控件)。对于时间...

    疯狂Android讲义源码

     2.5.5 使用DatePickerDialog、TimePickerDialog 123  2.5.6 使用ProgressDialog创建进度  对话框 125  2.6 消息提示 127  2.6.1 使用Toast显示提示信息框 128  2.6.2 Notification的功能与用法 129  2.7 ...

    疯狂Android讲义.part2

    2.5.5 使用DatePickerDialog、TimePickerDialog 123 2.5.6 使用ProgressDialog创建进度 对话框 125 2.6 消息提示 127 2.6.1 使用Toast显示提示信息框 128 2.6.2 Notification的功能与用法 129 2.7 菜单 132 2.7.1 ...

    疯狂Android讲义.part1

    2.5.5 使用DatePickerDialog、TimePickerDialog 123 2.5.6 使用ProgressDialog创建进度 对话框 125 2.6 消息提示 127 2.6.1 使用Toast显示提示信息框 128 2.6.2 Notification的功能与用法 129 2.7 菜单 132 2.7.1 ...

    nativescript-datetimepicker:具有日期和时间选择字段的插件

    还有一个DateTimePicker类,提供了静态方法pickDate和pickTime ,可以调用它们以显示与字段相同的对话框选择器。 屏幕截图 安装 tns plugin add nativescript-datetimepicker 组态 无需其他配置! 用法 要在标记中...

    Android 开发技巧

    4.9.2、DatePickerDialog/TimePickerDialog 119 4.10、WEBVIEW 120 4.10.1、WebView的使用 120 4.11、SCROLLVIEW 121 4.11.1、ScrollView的使用 121 4.12、GRIDVIEW 124 4.12.1、GridView的使用 124 4.13、GAMEVIEW ...

Global site tag (gtag.js) - Google Analytics