DatePicker的使用(二)

package com.example.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import android.util.Log;

import java.text.DateFormat;
import java.util.Calendar;



public class MainActivity extends Activity {
	
	TimePicker timePicker;
	DatePicker datePicker;
	
	int hour, minute;
	int yr, month, day;
	
	static final int TIME_DIALOG_ID = 0;
	static final int DATE_DIALOG_ID = 1;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Calendar today = Calendar.getInstance();
		yr = today.get(Calendar.YEAR);
		month = today.get(Calendar.MONTH);
		day = today.get(Calendar.DAY_OF_MONTH);
		
		showDialog(DATE_DIALOG_ID);
		
		timePicker = (TimePicker)findViewById(R.id.timePicker);
		timePicker.setIs24HourView(true);
		
		datePicker = (DatePicker)findViewById(R.id.datePicker);
		Button btnOpen = (Button)findViewById(R.id.btnSet);
		btnOpen.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v)
			{
				Toast.makeText(getBaseContext(), "Date Selected:" + datePicker.getMonth() + 1 + "/"
						+ datePicker.getDayOfMonth() + "/" + 
						datePicker.getYear() + "\n" + 
						"Time selected:" + timePicker.getCurrentHour() + ":" +
						timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show();
			}
		});
	}	
	
	
	protected Dialog onCreateDialog(int id)
	{
		switch (id) {
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, mTimeSetListener, hour, minute, false);
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, yr, month, day);
		}
		
		return null;
	}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			// TODO Auto-generated method stub
			yr = year;
			month = monthOfYear;
			day = dayOfMonth;
			
			Toast.makeText(getBaseContext(), "You have selected:" + (month + 1) + "/" + day + "/" + year, Toast.LENGTH_SHORT).show();
		}
	};
	
	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
			// TODO Auto-generated method stub
			hour = hourOfDay;
			minute = minuteOfHour;
			Toast.makeText(getBaseContext(), "You have selected: " + hour + ":" + minute, Toast.LENGTH_SHORT).show();
		}
	};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
<DatePicker android:id="@+id/datePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />  
    
<TimePicker android:id="@+id/timePicker" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />  
        
<Button android:id="@+id/btnSet"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="I am all set!" />
         
</LinearLayout>


你可能感兴趣的:(DatePicker的使用(二))