Android中DatePickerDialog和TimePickerDialog使用示例

MainActivity如下:

package cc.test;

import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
/**
 * Demo描述:
 * DatePickerDialog和TimePickerDialog使用示例
 * 
 * 参考资料:
 * http://blog.csdn.net/lilu_leo/article/details/7282272
 * Thank you very much
 * 
 */
public class TestDatePickerAndTimePickerActivity extends Activity {
	public int year;  
	public int month;  
	public int day;  
	public int hour;  
	public int minute; 
	public Calendar calendar;
	private Button mDatePickerButton;
	private Button mTimePickerButton;
    public final static int DATE_PICKER=9527;
    public final static int TIME_PICKER=9528;
    public DatePickerDialog mDatePickerDialog;
    public TimePickerDialog mTimePickerDialog;
    private DatePickerDialog.OnDateSetListener mDateSetListener;
    private TimePickerDialog.OnTimeSetListener mTimeSetListener;
    public boolean isCreatePickerDialog=true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
    
    private void init(){
    	mDatePickerButton=(Button) findViewById(R.id.datePickButton);
    	mDatePickerButton.setOnClickListener(new OnClickListenerImpl());
    	
    	mTimePickerButton=(Button) findViewById(R.id.timePickButton);
    	mTimePickerButton.setOnClickListener(new OnClickListenerImpl());
    	
    	//监听DatePicker的set按钮
        mDateSetListener=new DatePickerDialog.OnDateSetListener() {		
    		@Override
    		public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
    			System.out.println("---> 设置后: year="+year+", month="+monthOfYear+",day="+dayOfMonth);			
    		}
    	};
    	
    	//监听TimePicker的set按钮
    	mTimeSetListener=new TimePickerDialog.OnTimeSetListener() {
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				System.out.println("---> 设置后: hour="+hourOfDay+",minute="+minute);
			}
		};
    	
    }
    
    
	private class OnClickListenerImpl implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.datePickButton:
                showDialog(DATE_PICKER);
				break;
			case R.id.timePickButton:
				showDialog(TIME_PICKER);
				break;
			default:
				break;
			}
		}
	}
	
	
	
	
	//覆写Activty的onCreateDialog(int id)方法
	//注意:
	//1 第一次显示pickerDialog的时候会调用onCreateDialog()和onPrepareDialog()
	//2  随后每次只会调用onPrepareDialog()方法
	//  所以需要在onPrepareDialog()方法中获取最新时间再设置
	@Override
	protected Dialog onCreateDialog(int id) {
		System.out.println("---> onCreateDialog()");
		getCurrentTime();
		switch (id) {
		case DATE_PICKER:
			mDatePickerDialog=new DatePickerDialog(this, mDateSetListener, year, month, day);
			return mDatePickerDialog;
		case TIME_PICKER:
			mTimePickerDialog=new TimePickerDialog(this, mTimeSetListener, hour, minute, true);
			return mTimePickerDialog;
		default:
			break;
		}
		return super.onCreateDialog(id);
	}
	
	//覆写Activty的onPrepareDialog(int id, Dialog dialog)方法
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		if (!isCreatePickerDialog) {
			getCurrentTime();
			switch (id) {
			case DATE_PICKER:
				mDatePickerDialog.updateDate(year, month, day);
				break;
			case TIME_PICKER:
				mTimePickerDialog.updateTime(hour, minute);
				break;
			default:
				break;
			}
		}
		isCreatePickerDialog = false;
	}
	
	
	
	  private void getCurrentTime(){
	    	//设置时间为中国  
	    	calendar = Calendar.getInstance(Locale.CHINA);  
	    	//获取日期  
	    	year = calendar.get(Calendar.YEAR);  
	    	month = calendar.get(Calendar.MONTH);  
	    	day = calendar.get(Calendar.DAY_OF_MONTH);  
	    	hour = calendar.get(Calendar.HOUR_OF_DAY);  
	    	minute = calendar.get(Calendar.MINUTE); 
	    	System.out.println("-----> 获取当前时间 year="+year+",month="+month+",day="+day+",hour="+hour+",minute="+minute);
	    }
	
}


 

main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/datePickButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="DatePickButton" 
        android:layout_marginTop="100dip"
      />
    
    <Button
        android:id="@+id/timePickButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TimePickButton" 
        android:layout_marginTop="100dip"
      />

</LinearLayout>

Android中DatePickerDialog和TimePickerDialog使用示例

上一篇:Photoshop滤镜简单制作诡异的水纹


下一篇:Photoshop 艳丽的金属边框字效果