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. 简单使用CSS3实现炫酷读者墙效果

    读者墙,在很多网站上都有,没有遇到过的,可以参考度娘:读者墙http://www.baidu.com/s?wd=%B6%C1%D5%DF%C7%BD 使用基础的Html和CSS写出雏形 需要一提的是头 ...

  2. scala 学习笔记六 推导

    1.介绍 在Scala中,推导将生成器.过滤器.和定义组合在一起. 2.例子 有一种将result用作val(而不是var)的方式,:“就地”构建result,而不是逐项构建,利用yield关键字,当 ...

  3. Android -- Fragment注意事项

    ViewPager+Fragment 让Fragment成为ViewPager的一页时,FragmentManager会一直保存管理创建好了的Fragment,即使当前不是显示的这一页,Fragmen ...

  4. cognos report利用文本框提示优化日期维度

    为了尽量减少手工对日期维度的维护,在日期维度表中年份已经到了2099年,把年份作为下拉框或者月份作为下拉框的时候,选择起来颇为麻烦(当然也可以在此基础之上设置默认为当前月) 如图:提示页面以及html ...

  5. C++数据结构面试题

    原文:http://1527zhaobin.iteye.com/blog/1537110 一.判断链表是否存在环型链表问题:   说明:判断一个链表是否存在环,例如下面这个链表就存在环,n1--> ...

  6. _com_util::ConvertBSTRToString的使用问题

    #include <comutil.h> 然后调用_com_util::ConvertBSTRToString提示如下错误: error LNK2019: unresolved exter ...

  7. ArcGIS Pro体验03——主界面

    主界面和Office类似,应该上手比较快.新建Map后,加载了美国地图,毕竟是ESRI公司嘛.除项目(PROJECT),另外还有六个菜单面板,分别是地图(MAP).插入(INSERT).分析(ANAL ...

  8. angular cli中的--flat参数

    添加--flat 参数,表示不创建单独的目录 例如 ng g c news/news-detail 会将文件创建在app/news/news-detail目录下 ng g c news/news-de ...

  9. nm 命令 程序符号信息查看

    http://www.cnblogs.com/wangkangluo1/archive/2012/07/02/2572438.html 用途 显示关于对象文件.可执行文件以及对象文件库里的符号信息. ...

  10. 第十一节,命名空间namespace

    1,命名空间的定义 命名空间可以把不同的方法分散到不同的文件去实现,如果你会objective-C,他的作用和里面的类目有异曲同工之妙.当然了也有很多不同的地方,首先要明白的是,命名空间并不是一个类, ...