Android 中日期对话框的应用
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; } } };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)