DatePickerDialog和TimePickerDialog使用示例

简介: MainActivity如下: package cc.test;import java.util.Calendar;import java.

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>

相关文章
|
7月前
|
网络性能优化 调度 网络虚拟化
配置HQoS示例
HQoS简介 HQoS通过多级队列进一步细化区分业务流量,对多个用户、多种业务等传输对象进行统一管理和分层调度,在现有的硬件环境下使设备具备内部资源的控制策略,既能够为高级用户提供质量保证,又能够从整体上节约网络建设成本。 交换机的HQoS主要通过流队列和用户队列实现。
121 7
|
4月前
|
搜索推荐
示例
【8月更文挑战第27天】示例。
33 2
|
7月前
|
传感器 数据处理
示例三、光照度测试仪
示例三、光照度测试仪
58 1
一个完整的 ParentDataWidget 示例
一个完整的 ParentDataWidget 示例
132 0
一个完整的 ParentDataWidget 示例
|
XML Java 数据格式
SpringRMI解析1-使用示例
Java远程方法调用,即JavaRMI(Java Remote Method Invocation),是Java编程语言里一种用于实现远程过程调用的应用程序编程接口。它使客户机上的运行的程序可以调用远程服务器上的对象。
783 0