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

来源:互联网 发布:便宜又好用的鼠标知乎 编辑:程序博客网 时间:2024/05/18 11:18


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{TextViewm_TextView;//声明dataPickerDatePickerm_DatePicker;//声明TimePickerTimePickerm_TimePicker;Button m_dpButton;Button m_tpButton;//java中的Calendar类Calendar c;/** Called when the activity is first created. */@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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>


	
				
		
原创粉丝点击