【Android】时间与日期Widget(DatePicker 与 TimePicker)


public class

DatePicker

extends  FrameLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.DatePicker

Class Overview

This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or a CalendarView. 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 the Date Picker tutorial.

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

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


public class

TimePicker

extends  FrameLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.TimePicker

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, see TimePickerDialog.

See the Time 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>




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值