Android 中日期对话框的应用

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;  
           }  

       }  
    };  
Android 中日期对话框的应用

Android 中日期对话框的应用,布布扣,bubuko.com

Android 中日期对话框的应用

上一篇:Android SharedPreferences的应用


下一篇:关于自定义缓存ehcache使用时的导包错误