import java.util.Calendar;
import android.widget.DatePicker;
import android.app.DatePickerDialog; //日期对话框的标记 常量
private static final int DATE_DIALOG_ID = 1;
private static final int SHOW_DATAPICK = 0;
//日期变量
private int mYear;
private int mMonth;
private int mDay;
//当前操作的日期控件的标记
private String strDate_Tag=""; //购买日期
private EditText m_txtPurchaseDate = null;
private Button m_butPurchaseDate = null;
//安装日期
private EditText m_txtInstallationDate = null;
private Button m_butInstallationDate = null; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //购买日期
m_txtPurchaseDate = (EditText) findViewById(R.id.txtPurchaseDate);
m_butPurchaseDate = (Button) findViewById(R.id.butPurchaseDate);
//安装日期
m_txtInstallationDate= (EditText) findViewById(R.id.txtInstallationDate);
m_butInstallationDate = (Button) findViewById(R.id.butInstallationDate); initDate();//初始化 日期
} //初始化 日期
private void initDate(){ final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
//setDateTime();//设置日期
strDate_Tag="PurchaseDate";//购买日期
setDateTime();
strDate_Tag="InstallationDate";//安装日期
setDateTime();
}
//设置日期 为文本框 赋值
private void setDateTime() {
if(strDate_Tag=="PurchaseDate"){//购买日期
m_txtPurchaseDate.setText(new StringBuilder().append(mYear +"-").append(
(mMonth + 1) < 10 ? "0" + (mMonth + 1)+"-" : (mMonth + 1)+"-").append(
(mDay < 10) ? "0" + mDay : mDay));
}else if(strDate_Tag=="InstallationDate"){//安装日期
m_txtInstallationDate.setText(new StringBuilder().append(mYear +"-").append(
(mMonth + 1) < 10 ? "0" + (mMonth + 1)+"-" : (mMonth + 1)+"-").append(
(mDay < 10) ? "0" + mDay : mDay));
}
} //日期控件的事件
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
setDateTime(); //设置日期 为文本框 赋值
}
}; //购买日期Button的事件处理
class butPurchaseDateOnClickListener implements
android.view.View.OnClickListener {
public void onClick(View v) {
strDate_Tag="PurchaseDate";//当前点击的日期为 购买日期 String[] arrPurchaseDate =m_txtPurchaseDate.getText().toString().split("-");
mYear = Integer.parseInt(arrPurchaseDate[0]);
mMonth = Integer.parseInt(arrPurchaseDate[1]);
mDay = Integer.parseInt(arrPurchaseDate[2]); Message msg = new Message();
if (m_butPurchaseDate.equals((Button) v)) {
msg.what = MainActivity.SHOW_DATAPICK;
}
MainActivity.this.saleHandler.sendMessage(msg);
}
}
//购买日期Button的事件处理
class butInstallationDateOnClickListener implements
android.view.View.OnClickListener {
public void onClick(View v) {
strDate_Tag="InstallationDate";//当前点击的日期为 安装日期 String[] arrInstallationDate =m_txtInstallationDate.getText().toString().split("-");
mYear = Integer.parseInt(arrInstallationDate[0]);
mMonth = Integer.parseInt(arrInstallationDate[1]);
mDay = Integer.parseInt(arrInstallationDate[2]); Message msg = new Message();
if (m_butInstallationDate.equals((Button) v)) {
msg.what = MainActivity.SHOW_DATAPICK;
}
MainActivity.this.saleHandler.sendMessage(msg);
}
} //创建对话框 日期
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID: //日期
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
} protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
} //处理日期控件的Handler
Handler saleHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MainActivity.SHOW_DATAPICK:
showDialog(DATE_DIALOG_ID);
break;
}
}
};

Android 中日期对话框的应用的更多相关文章

  1. 9.Android之日期对话框DatePicker控件学习

    设置日期对话框在手机经常用到,今天来学习下. 首先设置好布局文件:如图 xml对应代码 <?xml version="1.0" encoding="utf-8&qu ...

  2. Android中的对话框AlertDialog使用技巧合集-转载

    Android中的对话框AlertDialog使用技巧合集     文章来自:http://blog.csdn.net/blue6626/article/details/6641105   今天我用自 ...

  3. Android中Dialog对话框的调用及监听

    Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...

  4. Android中Dialog对话框

    布局文件xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  5. android中常见对话框之一AlertDialog

    在Android应用中,有多种对话框:Dialog.AlertDialog.ProgressDialog.时间.日期等对话框. (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽 ...

  6. android中提示&对话框----ProgressDialog&DatePickerDialog &TimePickerDialog&PopupWindow

    ProgressDialog(精度条对话框): 1.直接调用ProgressDialog提供的静态方法show()显示 2.创建ProgressDialog,再设置对话框的参数,最后show()出来 ...

  7. $Android中日期和时间选择器的实现

    创建日期或时间选择窗口需要弹出Dialog的时候,Activity类的showDialog方法已经弃用了,而推荐使用的是DialogFragment,本文总结一下其具体用法. (一)日期选择器 1.创 ...

  8. java android中日期时间 问题总结

    Date 类型: Date date = new Date();   // 代表获取当前系统日期和时间 System.out.println(date); 使用类的方法设置时间和日期:(通过该方法初始 ...

  9. android中提示&对话框----Notification

    Notification(状态栏通知) 一.Notification用于状态栏显示通知的控件,在不同的设备上面Notification是不一样的 二.Notification的基本布局 元素组成: I ...

随机推荐

  1. C/C++ 语言获取文件大小

    在C语言中测试文件的大小,主要使用二个标准函数. 1.fseek 函数原型:int fseek ( FILE * stream, long int offset, int origin ); 参数说明 ...

  2. DIV+CSS自适应窗口高度

    <html> <head> <title>DIV+CSS自适应窗口高度</title> <style type="text/css&qu ...

  3. navigator.geolocation在IOS10系统中无法定位问题

    在使用地图需要定位的朋友都可能遇到这个问题,参考的文章说的是用百度地图,我用的是腾讯地图,但是其中的原理差不多.所以谢谢这些提供资源的大神们. if (navigator.geolocation) { ...

  4. 【SQLite】 C#中操作SQlite

    简介 SQLite是轻量级数据库,具体的介绍请参考官网(SQLite官网).在WinForm的项目中需要采用独立的数据库访问,可在断网单机上使用,所以选择了SQLite! 使用 主要针对C#项目(Wi ...

  5. C# GDI+技术

    C# GDI+技术 GDI+概述         GDI+是GDI(即Windows早期版本号中附带的Graphics Device Interface)的后继者.它是一种构成Windows XP操作 ...

  6. 【C#】SQL数据库助手类1.0(自用)

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...

  7. ArcGIS Pro体验01——申请、下载、安装

    ArcGIS Pro采用了Ribbon界面风格,看起来好漂亮,听起来很强大,就是不知道用起来怎么样,在网上看到一个ArcGIS Pro Beta2版本,下载下来,安装启动,好眼熟,像Office201 ...

  8. 新浪微博SSO登陆机制(转载)

    原文地址: http://www.cnblogs.com/AloneSword/p/3840548.html 最近在使用sina微博时,经常性交替使用 weibo.com 和 t.sina.cm.cn ...

  9. ZH奶酪:JavaScript清空数组的三种方法

    参考链接:http://snandy.iteye.com/blog/893955 方式1,length赋值为0 目前 Prototype中数组的 clear 方法和mootools库中数组的 empt ...

  10. Hibernate(六)一对多映射(多对一)

    一.Hinbernate中持久化类的关联关系 在数据库中,表表之间是通过外键关联的,在程序中是要转化为持久化类也就是(JAVA Bean)来实例的. 但在Hibernater中持久化的之间的映射关系, ...