zoukankan      html  css  js  c++  java
  • 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;  
               }  
           }  
        };  
  • 相关阅读:
    CAD迷你看图
    CAD打开文件总是弹出要求选择字体怎么办
    CAD字体显示问号的解决办法
    如何让IE8的菜单栏调到最上方
    苹果公司在 1980 年上市时为什么不使用双层股权架构来确保乔布斯有着足够的控制权?
    怪不的软件开发这么挣钱,原来是有这么多职位
    剖析余额宝“好”与“坏”
    网上盗刷事件频发 风险肇始于“快捷支付”?
    招商银行网银专业版怎么消除安全隐患
    《浅谈磁盘控制器驱动》,磁盘控制器驱动答疑解惑![2012.1.29完结]by skyfree
  • 原文地址:https://www.cnblogs.com/hailexuexi/p/3583681.html
Copyright © 2011-2022 走看看