布局:

View Code

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     android:orientation="vertical" > 6  7     <TextView 8         android:id="@+id/dateText" 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         />12     <Button 13         android:id="@+id/dateButton"14         android:layout_width="fill_parent"15         android:layout_height="wrap_content"16         android:text="设置日期"17         />18     <TextView19         android:id="@+id/timeText"20         android:layout_width="wrap_content"21         android:layout_height="wrap_content"22         />23     <Button 24         android:id="@+id/timeButton"25         android:layout_width="fill_parent"26         android:layout_height="wrap_content"27         android:text="设置时间"28         />29     30 </LinearLayout>

代码:

View Code

  1 package com.tiantian.test;  2   3 import java.util.Calendar;  4   5 import android.app.Activity;  6 import android.app.DatePickerDialog;  7 import android.app.DatePickerDialog.OnDateSetListener;  8 import android.app.Dialog;  9 import android.app.TimePickerDialog; 10 import android.app.TimePickerDialog.OnTimeSetListener; 11 import android.content.Intent; 12 import android.graphics.Bitmap; 13 import android.graphics.BitmapFactory; 14 import android.graphics.Matrix; 15 import android.net.Uri; 16 import android.os.Bundle; 17 import android.os.SystemClock; 18 import android.util.DisplayMetrics; 19 import android.view.View; 20 import android.view.View.OnClickListener; 21 import android.widget.Button; 22 import android.widget.Chronometer; 23 import android.widget.DatePicker; 24 import android.widget.DatePicker.OnDateChangedListener; 25 import android.widget.ImageView; 26 import android.widget.TextView; 27 import android.widget.TimePicker; 28 import android.widget.TimePicker.OnTimeChangedListener; 29 import android.widget.ZoomControls; 30  31 public class TextAndridActivity extends Activity { 32     /** Called when the activity is first created. */ 33     private TextView dateText; 34     private TextView timeText; 35     private Button dateButton; 36     private Button timeButton; 37      38     private int year; 39     private int monthOfYear; 40     private int dayOfMonth; 41     private int hour; 42     private int minute; 43     //声明一个独一无二的标识,来作为要显示DatePicker的Dialog的ID 44     final static int DATE_DIALOG_ID = 1; 45     //声明一个独一无二的标识,来作为要显示TimePicker的Dialog的ID 46     final static int TIME_DIALOG_ID = 2; 47     @Override 48     public void onCreate(Bundle savedInstanceState) { 49         super.onCreate(savedInstanceState); 50         setContentView(R.layout.main); 51          52         dateText = (TextView) findViewById(R.id.dateText); 53         timeText = (TextView) findViewById(R.id.timeText); 54         dateButton = (Button) findViewById(R.id.dateButton); 55         dateButton.setOnClickListener(new OnClickListener() { 56              57             @Override 58             public void onClick(View v) { 59                 //调用Activity类的方法来显示Dialog:调用这个方法会允许Activity管理该Dialog的生命周期,   60 //并会调用 onCreateDialog(int)回调函数来请求一个Dialog 61                 showDialog(DATE_DIALOG_ID); 62             } 63         }); 64         timeButton = (Button) findViewById(R.id.timeButton); 65         timeButton.setOnClickListener(new OnClickListener() { 66              67             @Override 68             public void onClick(View v) { 69                 //调用Activity类的方法来显示Dialog:调用这个方法会允许Activity管理该Dialog的生命周期,   70 //并会调用 onCreateDialog(int)回调函数来请求一个Dialog 71                 showDialog(TIME_DIALOG_ID); 72             } 73         }); 74         //得到当前日期和时间(作为初始值) 75         Calendar calendar = Calendar.getInstance(); 76         year = calendar.get(Calendar.YEAR); 77         monthOfYear = calendar.get(Calendar.MONTH); 78         dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); 79         hour = calendar.get(Calendar.HOUR_OF_DAY); 80         minute = calendar.get(Calendar.MINUTE); 81          82     } 83      84     //当Activity调用showDialog函数时会触发该函数的调用: 85     @Override 86     protected Dialog onCreateDialog(int id) { 87         // TODO Auto-generated method stub 88         switch(id){ 89         case DATE_DIALOG_ID: 90             return new DatePickerDialog(this, new OnDateSetListener() { 91                  92                 @Override 93                 public void onDateSet(DatePicker view, int year, int monthOfYear, 94                         int dayOfMonth) { 95                     // TODO Auto-generated method stub 96                     dateText.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日"); 97                 } 98             }, year, monthOfYear, dayOfMonth); 99         case TIME_DIALOG_ID:100             return new TimePickerDialog(this, new OnTimeSetListener() {101                 102                 @Override103                 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {104                     // TODO Auto-generated method stub105                     timeText.setText(hourOfDay + "时" + minute + "分");106                 }107             }, hour, minute, true);108         }109         return null;110     }111     112     113     114 }

android中DatePickerTimePicker的应用相关推荐

  1. Android中实现为TextView添加多个可点击的文本

    这篇文章主要介绍了Android中实现为TextView添加多个可点击的文本,可实现类似Android社交软件显示点赞用户并通过用户名称进入该用户主页的功能,是非常实用的技巧,需要的朋友可以参考下.具 ...

  2. android 弹出fragment,Android中ViewPager获取当前显示的Fragment

    前言 在项目中,有时会用到在ViewPager中显示同样类型的Fragment,同时这样的Fragment的个数是动态的,但是PagerAdapter没有给我们提供getCurrentFragment ...

  3. android 读取内部存储文件格式,Android中的数据储存之文件存储

    当我们在使用各种程序时,其实际上是在和各种数据打交道,当我们聊QQ,刷微博,看新闻,其实都是在和里面的数据交互 例如在聊天时发出的消息,以及在登录时输入的账号密码,其实都是瞬时数据,那什么是瞬时数据呢 ...

  4. android中一种不支持的lua操作

    今天写了一段lua代码,在win32中正常运行,在android中运行无效. 大概是这样的: ------file1.lua----- local t = {} t.str = "this ...

  5. Android中对Log日志文件的分析[转]

    一,Bug出现了, 需要"干掉"它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只 ...

  6. 在Android中进行单元测试遇到的问题

    问题1.Cannot connect to VM  socket closed 在使用JUnit进行测试的时候,遇到这个问题.网上的解释是:使用Eclipse对Java代码进行调试,无论是远程JVM还 ...

  7. android中设置控件获得焦点 (转)

    android中,要使控件获得焦点,需要先setFocus,再requestFocus. 以Button为例:                 btn.setFocusable(true);      ...

  8. Android中怎么使图片显示

    android中经常到利用图片,利用图片时我们都是把图片放在drawable-xxx目录下,因为这里就是存储图片的地方,xxx不用理他,就是关于分辨率而已. 楼主在学习android的时候一直尝试把图 ...

  9. Android中处理崩溃异常

    2019独角兽企业重金招聘Python工程师标准>>> 大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象 ...

最新文章

  1. JAVASCRIPT复制到剪贴板
  2. LIME算法:模型的可解释性(代码实现)
  3. Regex 正则零宽断言
  4. 03SpringMVC,Spring,Hibernate整合(Date时间转换)
  5. Oracle rowid
  6. php类常量的特点,php类常量是什么?类常量用法详解
  7. DNS A记录和CNAME记录
  8. 苹果css攻击,研究人员设计了一种新的CSS和HTML攻击 导致iPhone重启或冻结Mac
  9. junit 经典示例_JUnit 4,JWebUnit,Arquillian和JSF单元示例教程
  10. c#oracle数组超出,急问:System.IndexOutOfRangeException: 索引超出了数组界限解决方案
  11. MySQL数据库介绍
  12. ESD笔记(三)_常用ESD保护电路优缺点
  13. 十八、DPM模型案例(二)
  14. 高斯投影正反算C语言程序代码,高斯投影正反算c代码
  15. Anaconda——最省心的 Python 版本
  16. 去除网页腾讯 优酷 爱奇艺右上角logo水印
  17. 电磁学基本知识与基本定律复习
  18. android 语音和输入法按钮切换,android 切换系统语言,输入法也随之切换
  19. gitlab邮件发不出去解决方法
  20. Premiere导出视频音画不同步的解决方案

热门文章

  1. sts无法创建java_java – STS无法在我的机器上启动
  2. python requests模块_Python 爬虫教程 requests 模块
  3. Arduino可穿戴开发入门教程(大学霸内部资料)
  4. Ie html button消失,input 按钮在IE下兼容问题
  5. c++向量和数组的区别_数学学习如此容易:用Python计算特征值和特征向量
  6. 一个html文档所需要的最基本的标记对是,川大《WEB技术》19秋在线作业1题目【标准答案】...
  7. arch linux 下 安装搭建python机器学习环境
  8. 全国计算机二级计基础题第十五套,计算机等级考试:二级VFP机试第15套
  9. mysql中00933错误_java.sql.SQLException: ORA-00933: SQL 命令未正确结束错误解决
  10. 给PS4装上Linux,然后用Xbox手柄打任天堂游戏?网友:这场面我没见过