DatePicker

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   <DatePicker 
       android:id="@+id/dataPicker"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       
       />
</LinearLayout>

 

 DatePicker中的点击改变日期事件是用:

 

datePicker.init(mYear, mMonth, mDay,OnDateChangedListener)

 

public class DatePickerActivity extends Activity {
   private DatePicker datePicker; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        datePicker=(DatePicker)findViewById(R.id.dataPicker);
        final Calendar calendar=Calendar.getInstance();
        int mYear=calendar.get(Calendar.YEAR);
        int mMonth=calendar.get(Calendar.MONTH)+1;
        int mDay=calendar.get(Calendar.DAY_OF_MONTH);
        datePicker.init(mYear, mMonth, mDay,new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				Toast.makeText(DatePickerActivity.this, year+"-"+monthOfYear+"-"+dayOfMonth, Toast.LENGTH_LONG).show();
			}
		});
    }
}

DatePicker

 

 

 

DatePickerDialog:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <TextView 
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center_horizontal"
		android:textSize="20sp"
		android:text=""
        />
    <Button 
        android:id="@+id/btnPickDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="修改时间"
        />
</LinearLayout>

 public class DatePickerActivity extends Activity {

 

   protected static final int DATA_DIALOG_ID = 0;
   private int mYear;
   private int mMonth;
   private int mDay;
   private Button btnPickDate;
   private TextView tv01;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        btnPickDate=(Button)findViewById(R.id.btnPickDate);
        btnPickDate.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
					showDialog(DATA_DIALOG_ID);    
					//activity下的    showDialog方法,点击按钮时会调用onCreateDialog方法
			}
		});
        final Calendar calendar=Calendar.getInstance();
        mYear=calendar.get(Calendar.YEAR);
        mMonth=calendar.get(Calendar.MONTH)+1;
        mDay=calendar.get(Calendar.DAY_OF_MONTH);
    }
	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id){
		  case DATA_DIALOG_ID: //根据用户传递的id来判断
			  return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
		}
		return null;
		
	}
	//点击设置
	private DatePickerDialog.OnDateSetListener mDateSetListener=new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear=year;
			mMonth=monthOfYear;
			mDay=dayOfMonth;
			updateDisplay();//显示到textview中
			
		}
	};
    
   private void updateDisplay(){
	   tv01=(TextView)findViewById(R.id.tv1);
	   tv01.setText(new StringBuffer().append("时间: ").append(mYear).append("-").append(mMonth+1).append("-").append(mDay));
   }
}

 

  DatePicker

 

 TimePickerDialog:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tv01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />
    <Button
        android:id="@+id/btnTimeDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置时间"
         android:layout_gravity="center_horizontal"
         />
</LinearLayout>

 public class TimePickerDialogActivity extends Activity {

 

  protected static final int TIME_DIALOG_ID = 0;
private Button btnTimeDialog;
  private TextView tv01;
  private int mHour;
  private int mMinnte;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv01=(TextView) findViewById(R.id.tv01);
        btnTimeDialog=(Button) findViewById(R.id.btnTimeDialog);
        
        btnTimeDialog.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID);
			}
		});
        
        final Calendar calendar=Calendar.getInstance();
        mHour=calendar.get(Calendar.HOUR_OF_DAY);
        mMinnte=calendar.get(Calendar.MINUTE);
        updateDisplay();
    }



	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id){
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this,mTimeSetListener,mHour,mMinnte,true);//true表示24小时制
		}
		return null;
	}
	
	private TimePickerDialog.OnTimeSetListener mTimeSetListener=new TimePickerDialog.OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour=hourOfDay;
			mMinnte=minute;
			updateDisplay();
		}
	};
    private void updateDisplay(){
    	tv01.setText(new StringBuffer().append("时间: ").append(pad(mHour)).append(":").append(pad(mMinnte)));
    }
    private static String pad(int c){
    	if(c>=10){
    		return String.valueOf(c);
    	}
    	else{
    		return "0"+String.valueOf(c);
    	}
    }
}

  DatePicker

 

 

 

你可能感兴趣的:(Datepicker)