Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • android的DatePickerDialog和TimePickerDialog

    post by onelong / 2010-5-30 3:28 Sunday [android]

    package com.hl;

    import java.util.Calendar;

    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.app.TimePickerDialog;
    import android.app.DatePickerDialog.OnDateSetListener;
    import android.app.TimePickerDialog.OnTimeSetListener;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.TextView;
    import android.widget.TimePicker;

    public class MyDateAndTime extends Activity {
        private Calendar _date;
        private TextView txt;
        private final int DIALOG_DATEPICK=1;
        private final int DIALOG_TIMEPICK=2;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init();
        }
        private void init(){   
         _date=Calendar.getInstance();
         txt=(TextView)findViewById(R.id.show);
         
         /*
          * 以下一种常用的方法
          * android.view.View.OnClickListener dateClick=new android.view.View.OnClickListener(){
       @Override
       public void onClick(View v) {
        showDialog(DIALOG_DATEPICK); 
       }
      };
      android.view.View.OnClickListener timeClick=new android.view.View.OnClickListener(){
       @Override
       public void onClick(View v) {
        showDialog(DIALOG_TIMEPICK); 
       }
      };
         Button mydate=(Button)findViewById(R.id.mydate);
         mydate.setOnClickListener(dateClick);
         Button mytime=(Button)findViewById(R.id.mytime);
         mytime.setOnClickListener(timeClick);
         */
         
         MyCallBack click=new MyCallBack();
         Button mydate=(Button)findViewById(R.id.mydate);
         mydate.setOnClickListener(click);
         Button mytime=(Button)findViewById(R.id.mytime);
         mytime.setOnClickListener(click);
        }
        @Override
        protected Dialog onCreateDialog(int id){
         switch(id){
         case DIALOG_DATEPICK:
          return new DatePickerDialog(this, new OnDateSetListener(){
           @Override
           public void onDateSet(DatePicker v,int y,int m,int d){
            txt.setText("设定的日期是:"+String.valueOf(y)+"年"+String.valueOf(m)+"月"+String.valueOf(d)+"日");
           }
          },
          _date.get(Calendar.YEAR),//获得系统的当前年份
          _date.get(Calendar.MONTH),
          _date.get(Calendar.DAY_OF_WEEK_IN_MONTH)
          );
         case DIALOG_TIMEPICK:
          return new TimePickerDialog(this, new OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
         txt.setText("设定的时间是:"+String.valueOf(hourOfDay)+"时"+String.valueOf(minute)+"分");
        }
       },
       _date.get(Calendar.HOUR_OF_DAY),
       _date.get(Calendar.MINUTE),
       true
       );
         default:
          return null;
         }
        }
        //定义内部私有类实现android.view.View.OnClickListener这个静态的接口
        private class MyCallBack implements android.view.View.OnClickListener{
         @Override
         public void onClick(View v) {
          switch(v.getId()){
          case R.id.mydate:
           MyDateAndTime.this.showDialog(DIALOG_DATEPICK);
           break;
          case R.id.mytime:
           MyDateAndTime.this.showDialog(DIALOG_TIMEPICK);
           break;       
          } 
         }
        }
    }


    附件下载:
    MyDateAndTime.rar 42.61KB

    引用地址:
     

    我要评论