【Android】時間與日期Widget(DatePicker 與 TimePicker)

iteye_20954發表於2011-12-26


public class

DatePicker

extendsFrameLayout

Class Overview

This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or aCalendarView. The set of spinners and the calendar view are automatically synchronized. The client can customize whether only the spinners, or only the calendar view, or both to be displayed. Also the minimal and maximal date from which dates to be selected can be customized.

See theDate Picker tutorial.

To provide a widget for selecting a date, use theDatePickerwidget, which allows the user to select the month, day, and year, in a familiar interface.

In this tutorial, you'll create aDatePickerDialog, which presents the date picker in a floating dialog box at the press of a button. When the date is set by the user, aTextViewwill update with the new date.


public class

TimePicker

extendsFrameLayout

Class Overview

A view for selecting the time of day, in either 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (if applicable) can be conrolled by vertical spinners. The hour can be entered by keyboard input. Entering in two digit hours can be accomplished by hitting two digits within a timeout of about a second (e.g. '1' then '2' to select 12). The minutes can be entered by entering single digits. Under AM/PM mode, the user can hit 'a', 'A", 'p' or 'P' to pick. For a dialog using this view, seeTimePickerDialog.

See theTime Picker tutorial.



下面是一個轉載自書上的示例:

public class Activity01 extends Activity
{
	TextView	m_TextView;
	//宣告dataPicker
	DatePicker	m_DatePicker;
	//宣告TimePicker
	TimePicker	m_TimePicker;
	Button 		m_dpButton;
	Button 		m_tpButton;
	//java中的Calendar類
	Calendar 	c;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		c=Calendar.getInstance();
		
		m_TextView= (TextView) findViewById(R.id.TextView01);
		m_dpButton = (Button)findViewById(R.id.button1);
		m_tpButton = (Button)findViewById(R.id.button2);
		
		//獲取DataPicker物件
		m_DatePicker = (DatePicker) findViewById(R.id.DatePicker01);
		//初始化當前時間並設定監聽
		m_DatePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
			{
				//當日期更改在這裡設定
				c.set(year, monthOfYear, dayOfMonth);
			}
		});

		//
		m_TimePicker = (TimePicker) findViewById(R.id.TimePicker01);
		//
		m_TimePicker.setIs24HourView(true);

		//
		m_TimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
			{
				//當時間更改在這裡設定
				c.set(year, month, day, hourOfDay, minute, second);
			}
		});
		
		
		m_dpButton.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				new DatePickerDialog(Activity01.this,
					new DatePickerDialog.OnDateSetListener()
					{
						public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
						{
							//設定日曆
						}
					},c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
			}
		});
		
		m_tpButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v)
			{
				new TimePickerDialog(Activity01.this,
					new TimePickerDialog.OnTimeSetListener()
					{
						public void onTimeSet(TimePicker view, int hourOfDay,int minute)
						{
							//設定時間
						}
					},c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show();
			}
		});
	}
}




對應的layout檔案
<?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"
    >
	<TextView  
	android:id="@+id/TextView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <Button
      android:id="@+id/Button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
    >
    </Button>
    <Button
      android:id="@+id/Button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
    >
    </Button>
</LinearLayout>



 

相關文章