利用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. pytorch训练模型的一些坑

    1. 图像读取 opencv的python和c++读取的图像结果不一致,是因为python和c++采用的opencv版本不一样,从而使用的解码库不同,导致读取的结果不同. 详细内容参考:https:/ ...

  2. 《基于Scyther的秘钥建立协议设计》-------摘抄整理

    本篇论文额主要创新点:   利用Scyther软件,通过对一个不安全的秘钥建立协议逐步添加并验证安全属性,最终建立一个安全的秘钥建立协议. 通过形式化分析软件设计秘钥建立协议课可以提高协议设计效率,减 ...

  3. idou教你学Istio10 : 如何用Istio实现K8S Egress流量管理

    上一篇我们了解了如何控制入口流量,本文主要介绍在使用Istio时如何访问集群外服务,即对出口流量的管理. 默认安装的Istio是不能直接对集群外部服务进行访问的,如果需要将外部服务暴露给 Istio ...

  4. JAVA Calendar类获取上个月的第一天和最后一天

    原文:https://www.cnblogs.com/QQParadise/articles/4936313.html 获取上个月第一天的方法: Calendar calendar = Calenda ...

  5. Hive系统函数之collect_list和collect_set

    转自:https://www.cnblogs.com/cc11001100/p/9043946.html Hive中collect相关的函数有collect_list和collect_set. 它们都 ...

  6. 揭秘PHP深受Web开发者喜爱的原因

    我们再次回顾一下在软件开发的发展中非常有名的技术"PHP"(Hypertext Pre-Processor),它是由Rasmus Lerdorf在1995年发明的.开始阶段,PHP ...

  7. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things

    链接: https://codeforces.com/contest/1247/problem/A 题意: Kolya is very absent-minded. Today his math te ...

  8. 0、Spring 注解驱动开发

    0.Spring注解驱动开发 0.1 简介 <Spring注解驱动开发>是一套帮助我们深入了解Spring原理机制的教程: 现今SpringBoot.SpringCloud技术非常火热,作 ...

  9. Python 11--文件流

  10. GitHub 更新fork的代码

    转载地址:http://blog.csdn.net/do_it__/article/details/7836513 一.前提 本文的前提是你已经在github上fork了别人的分支,并且弄好了跟git ...