android 日期选择器(DatePicker)学习与应用 (转)
日期和时间选择对话框,首先是要获得当前时间,这里用 java类中的Calendar来获得日期和时间(也可以用Date,但是不提倡,Date部分方法已经注释为过时), Calendar是一个抽象类,是通过getInstance()来获得实例
日期和时间选择对话框无需在像前面一样借组与AlertDialog.Builder来作为内容的载体,有直接的Dialog,其实源码里面已经包括了Builder。
1、DatePickerDialog 是日期Dialog,如下建立
view plaincopy to clipboardprint?
dialog = new DatePickerDialog(this,
dateListener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dialog = new DatePickerDialog(this,
dateListener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
this 表示当前 Acitivity,表示这是当前Acitivity的对话框
dateListener为产生日期事件的监听器,这里是通过DatePickerDialog.OnDateSetListener来获得,代码如下
view plaincopy to clipboardprint?
DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker,
int year, int month, int dayOfMonth) {
EditText editText =
(EditText) findViewById(R.id.editText);
//Calendar月份是从0开始,所以month要加1
editText.setText("你选择了" + year + "年" +
(month+1) + "月" + dayOfMonth + "日");
}
};
DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker,
int year, int month, int dayOfMonth) {
EditText editText =
(EditText) findViewById(R.id.editText);
//Calendar月份是从0开始,所以month要加1
editText.setText("你选择了" + year + "年" +
(month+1) + "月" + dayOfMonth + "日");
}
};
这里注意的是calendar.getCalendar(Calendar.MONTH), Calendar类中是从0开始取月份的,但是这里DatePickerDialog已经做了处理。
2、TimePickerDialog为时间选择对话框,实现代码如下:
view plaincopy to clipboardprint?
dialog = new TimePickerDialog(this, timeListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
false); //是否为二十四制
dialog = new TimePickerDialog(this, timeListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
false); //是否为二十四制
timeListener 是一个TimePickerDialog.OnTimeSetListener 实例。
效果图如下:
这是日期选择对话框 这是时间选择对话框
实现步骤:
第一步:一个用来显示选择的内容的输入框和两个用来显示日期和时间的按钮
res/layout/date_time_dialog_layout.xml
view plaincopy to clipboardprint?
<?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"
>
<EditText android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"/>
<Button android:id="@+id/btnDate"
android:text="显示日期选择对话框"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/btnTime"
android:text="显示时间选择对话框"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?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"
>
<EditText android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"/>
<Button android:id="@+id/btnDate"
android:text="显示日期选择对话框"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/btnTime"
android:text="显示时间选择对话框"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
第二步:
在DateTimeDialogActivity的OnCreate方法里面要对两个button进行单击事件处理,OnCreateDialog来处理产生对话框
src/com/dialog/activity/DateTimeDialogActivity.java
view plaincopy to clipboardprint?
package com.dialog.activity;
import java.util.Calendar;
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.EditText;
import android.widget.TimePicker;
public class DateTimeDialogActivity extends Activity {
private final int DATE_DIALOG = 1;
private final int TIME_DIALOG = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_time_dialog_layout);
View.OnClickListener dateBtnListener =
new BtnOnClickListener(DATE_DIALOG);
Button btnDate = (Button) findViewById(R.id.btnDate);
btnDate.setOnClickListener(dateBtnListener);
View.OnClickListener timeBtnListener =
new BtnOnClickListener(TIME_DIALOG);
Button btnTime = (Button) findViewById(R.id.btnTime);
btnTime.setOnClickListener(timeBtnListener);
}
protected Dialog onCreateDialog(int id) {
//用来获取日期和时间的
Calendar calendar = Calendar.getInstance();
Dialog dialog = null;
switch(id) {
case DATE_DIALOG:
DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker,
int year, int month, int dayOfMonth) {
EditText editText =
(EditText) findViewById(R.id.editText);
//Calendar月份是从0开始,所以month要加1
editText.setText("你选择了" + year + "年" +
(month+1) + "月" + dayOfMonth + "日");
}
};
dialog = new DatePickerDialog(this,
dateListener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
break;
case TIME_DIALOG:
TimePickerDialog.OnTimeSetListener timeListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timerPicker,
int hourOfDay, int minute) {
EditText editText =
(EditText) findViewById(R.id.editText);
editText.setText("你选择了" + hourOfDay + "时" +
minute + "分");
}
};
dialog = new TimePickerDialog(this, timeListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
false); //是否为二十四制
break;
default:
break;
}
return dialog;
}
/*
* 成员内部类,此处为提高可重用性,也可以换成匿名内部类
*/
private class BtnOnClickListener implements View.OnClickListener {
private int dialogId = 0; //默认为0则不显示对话框
public BtnOnClickListener(int dialogId) {
this.dialogId = dialogId;
}
@Override
public void onClick(View view) {
showDialog(dialogId);
}
}
}