利用Android应用框架提供的DatePicker(日期选择器)和TimePicker(时间选择器),实现日期时间选择器。

Dialog的Content布局文件(date_time_dialog.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="请选择日期"
android:textColor="#FFFFFF"
android:textSize="16sp" /> <DatePicker
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip" /> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:singleLine="true"
android:text="请选择时间"
android:textColor="#FFFFFF"
android:textSize="16sp" /> <TimePicker
android:id="@+id/time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip" /> </LinearLayout>

代码中的实现:

package com.easipass.test;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker; /**
* 功能描述:实现日期时间选择器
*
* @author android_ls
*/
public class DateTimeActivity extends Activity implements View.OnTouchListener { private EditText etStartTime; private EditText etEndTime; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); etStartTime = (EditText) this.findViewById(R.id.et_start_time);
etEndTime = (EditText) this.findViewById(R.id.et_end_time); etStartTime.setOnTouchListener(this);
etEndTime.setOnTouchListener(this); } @Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) { AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = View.inflate(this, R.layout.date_time_dialog, null);
final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
final TimePicker timePicker = (android.widget.TimePicker) view.findViewById(R.id.time_picker);
builder.setView(view); Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null); timePicker.setIs24HourView(true);
timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(Calendar.MINUTE); if (v.getId() == R.id.et_start_time) {
final int inType = etStartTime.getInputType();
etStartTime.setInputType(InputType.TYPE_NULL);
etStartTime.onTouchEvent(event);
etStartTime.setInputType(inType);
etStartTime.setSelection(etStartTime.getText().length()); builder.setTitle("选取起始时间");
builder.setPositiveButton("确 定", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer();
sb.append(String.format("%d-%02d-%02d",
datePicker.getYear(),
datePicker.getMonth() + 1,
datePicker.getDayOfMonth()));
sb.append(" ");
sb.append(timePicker.getCurrentHour())
.append(":").append(timePicker.getCurrentMinute()); etStartTime.setText(sb);
etEndTime.requestFocus(); dialog.cancel();
}
}); } else if (v.getId() == R.id.et_end_time) {
int inType = etEndTime.getInputType();
etEndTime.setInputType(InputType.TYPE_NULL);
etEndTime.onTouchEvent(event);
etEndTime.setInputType(inType);
etEndTime.setSelection(etEndTime.getText().length()); builder.setTitle("选取结束时间");
builder.setPositiveButton("确 定", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer();
sb.append(String.format("%d-%02d-%02d",
datePicker.getYear(),
datePicker.getMonth() + 1,
datePicker.getDayOfMonth()));
sb.append(" ");
sb.append(timePicker.getCurrentHour())
.append(":").append(timePicker.getCurrentMinute());
etEndTime.setText(sb); dialog.cancel();
}
});
} Dialog dialog = builder.create();
dialog.show();
} return true;
} }

运行后的效果图:

点击EditTet之后

点击确定之后

http://blog.csdn.net/android_ls/article/details/8644067

Android中实现日期时间选择器(DatePicker和TimePicker)的更多相关文章

  1. 日期 时间选择器(DatePicker和TimePicker)实现用户选择

    日期和时间 作者的设计TimePicker时,大小分布不合理,我调整宽度为match-parent高度为wrap-parent就可以了. public class MainActivity exten ...

  2. Android日期时间选择器DatePicker、TimePicker日期时间改变事件响应(Android学习笔记)

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  3. Android中关于日期时间与时区的使用总结

    在开发Android的过程中,出现过几次由于日期时间导致的问题,而且主要是由于时区的原因导致,所以一直想总结一下,形成一个良好的开发规范.   一.Unix时间戳   Unix时间戳(Unix tim ...

  4. android 中 系统日期时间的获取

    import    java.text.SimpleDateFormat; SimpleDateFormat    formatter    =   new    SimpleDateFormat   ...

  5. Android日期时间选择器实现以及自定义大小

    本文主要讲两个内容:1.如何将DatePicker和TimePicker放在一个dialog里面:2.改变他们的宽度: 问题1:其实现思路就是自定义一个Dialog,然后往里面同时放入DatePick ...

  6. android日期时间选择器

    android原生的日期时间控件,因为是原生的总有其满足不了我们需求的时候,Android 手机版本那么多,用户弹出来的控件五花八门.因为项目需要,在网上找了一 些demo看了看,感觉有些写的很好,很 ...

  7. 第32讲 UI组件之 时间日期控件DatePicker和TimePicker

    第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置,    Time ...

  8. 24款最好的jQuery日期时间选择器插件

    如果你正在创建一个网络表单,有很多事情你需要在你的应用程序中使用.有时您需要特别的输入,从用户的日期和时间,如发票日期,生日,交货时间,或任何其他此类信息.如果你有这样的需要,可以极大地从动态的jQu ...

  9. Bootstrap-datepicker日期时间选择器的简单使用

    日期时间选择器 目前,bootstrap有两种日历.datepicker和datetimepicker,后者是前者的拓展. Bootstrap日期和时间组件: 使用示例: 从左到右依次是十年视图.年视 ...

随机推荐

  1. 04 Windows编程——Unicode

    VS 2017下源码 #include<stdio.h> int main() { char ASC_a = 'a'; char *ASC_str = "hello"; ...

  2. nmap中文帮助文档

    简介: Nmap(“ Network Mapper ”)是用于网络探索和安全审核的开源工具.它设计用于快速扫描大型网络,尽管它可以在单个主机上正常运行.Nmap以新颖的方式使用原始IP数据包来确定网络 ...

  3. 中国大学MOOC课程信息之数据分析可视化二

    版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82318571 - 写在前面 本篇博客继续对中国大学MOOC ...

  4. 关于阿里云OSS上传图片之后会被旋转90度的解决办法

    原文:https://www.cnblogs.com/wuhjbk/p/10133596.html 问题描述:正常的图片前端上传到oss成功之后的资源地址.在html上引用的时候被旋转了90度oss资 ...

  5. 双向绑定v-bind

    通过v-model绑定输出数据 <script> export default { data() { return { pagestyle:'https://v4.bootcss.com/ ...

  6. 12、生命周期-@Bean指定初始化和销毁方法

    12.生命周期-@Bean指定初始化和销毁方法 Bean的生命周期:创建->初始化->销毁 容器管理bean的生命周期 我们可以自定义初始方法和销毁方法,容器在bean进行到当期那生命周期 ...

  7. 9、Spring Boot 2.x 集成 Thymeleaf

    1.9 Spring Boot 2.x 集成 Thymeleaf 完整源码: Spring-Boot-Demos 1.9.1 在pom中引入依赖 <dependency> <grou ...

  8. Invalid HTTP_HOST header: 'xxx.xxx:8000'. You may need to add 'xxx.xx' to ALLOWED_HOSTS

    用python3 manage.py runserver 0.0.0.0:8000命令运行django程序后,通过浏览器访问服务器网址的8000端口,出现访问错误,报错为 Invalid HTTP_H ...

  9. Pollard-Rho 总结

    将一个大数\(N\)分解质因子. 试除法,暴力枚举\(1-\sqrt{N}\)的数.时间复杂度:\(O(\sqrt{N})\). 通常,这个复杂度够了,但有时,\(N\leq10^{18}\). 这就 ...

  10. 002_linux驱动之_register_chrdev注册字符设备

    (一)解析:register_chrdev函数和unregister_chrdev函数 (二)register_chrdev函数原型 int register_chrdev(unsigned int ...