Android 程式开发:(十一)选择控件 —— 11.2 DatePicker
DataPicker和上一节讲的TimePicker很相似。使用DatePicker,可以让用户选择一个特定的日期。下面展示如何使用DatePicker。
1、使用上一节的工程,BasicViews4,修改main.xml。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <Buttonandroid:id="@+id/btnSet"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Iamallset!"
- android:onClick="onClick"/>
- <DatePickerandroid:id="@+id/datePicker"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TimePickerandroid:id="@+id/timePicker"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
3、在BasicViews4Activity.java中添加一些代码
- packagenet.learn2develop.BasicViews4;
- importjava.text.SimpleDateFormat;
- importjava.util.Calendar;
- importjava.util.Date;
- importandroid.app.Activity;
- importandroid.app.DatePickerDialog;
- importandroid.app.Dialog;
- importandroid.app.TimePickerDialog;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.DatePicker;
- importandroid.widget.TimePicker;
- importandroid.widget.Toast;
- publicclassBasicViews4ActivityextendsActivity{
- TimePickertimePicker;
- DatePickerdatePicker;
- inthour,minute;
- intyr,month,day;
- staticfinalintTIME_DIALOG_ID=0;
- staticfinalintDATE_DIALOG_ID=1;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- timePicker=(TimePicker)findViewById(R.id.timePicker);
- timePicker.setIs24HourView(true);
- //showDialog(TIME_DIALOG_ID);
- datePicker=(DatePicker)findViewById(R.id.datePicker);
- //---getthecurrentdate---
- Calendartoday=Calendar.getInstance();
- yr=today.get(Calendar.YEAR);
- month=today.get(Calendar.MONTH);
- day=today.get(Calendar.DAY_OF_MONTH);
- showDialog(DATE_DIALOG_ID);
- }
- @Override
- protectedDialogonCreateDialog(intid)
- {
- switch(id){
- caseTIME_DIALOG_ID:
- returnnewTimePickerDialog(
- this,mTimeSetListener,hour,minute,false);
- caseDATE_DIALOG_ID:
- returnnewDatePickerDialog(
- this,mDateSetListener,yr,month,day);
- }
- returnnull;
- }
- privateDatePickerDialog.OnDateSetListenermDateSetListener=
- newDatePickerDialog.OnDateSetListener()
- {
- publicvoidonDateSet(
- DatePickerview,intyear,intmonthOfYear,intdayOfMonth)
- {
- yr=year;
- month=monthOfYear;
- day=dayOfMonth;
- Toast.makeText(getBaseContext(),
- "Youhaveselected:"+(month+1)+
- "/"+day+"/"+year,
- Toast.LENGTH_SHORT).show();
- }
- };
- privateTimePickerDialog.OnTimeSetListenermTimeSetListener=
- newTimePickerDialog.OnTimeSetListener()
- {
- publicvoidonTimeSet(
- TimePickerview,inthourOfDay,intminuteOfHour)
- {
- hour=hourOfDay;
- minute=minuteOfHour;
- SimpleDateFormattimeFormat=newSimpleDateFormat("hh:mmaa");
- Datedate=newDate(0,0,0,hour,minute);
- StringstrDate=timeFormat.format(date);
- Toast.makeText(getBaseContext(),
- "Youhaveselected"+strDate,
- Toast.LENGTH_SHORT).show();
- }
- };
- publicvoidonClick(Viewview){
- Toast.makeText(getBaseContext(),
- "Dateselected:"+(datePicker.getMonth()+1)+
- "/"+datePicker.getDayOfMonth()+
- "/"+datePicker.getYear()+"\n"+
- "Timeselected:"+timePicker.getCurrentHour()+
- ":"+timePicker.getCurrentMinute(),
- Toast.LENGTH_SHORT).show();
- }
- }