Android DatePickerDialog的应用举例
- package lxy.litsoft;
-
- import android.app.Activity;
- import android.app.DatePickerDialog;
- import android.app.Dialog;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.DatePicker;
-
- public class AppMain extends Activity {
-
- //声明一个Button对象
- Button disDialog;
- //创建一个常量,标识DatePickerDialog
- private static final int DATE_PICKER_ID = 3;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- disDialog = (Button)findViewById(R.id.button01);
- disDialog.setOnClickListener(new ButtonListener());
- }
-
- /**
- * 内部匿名类,实现DatePickerDialog.OnDateSetListener接口,重写onDateSet()方法
- * 当弹出DatePickerDialog并设置完Date以后,左下方有个“Set”按钮,表示确定设置。当这个
- * 按钮被点击的时候,就执行这里的onDateSet()方法。
- */
- DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
- public void onDateSet(DatePicker view, int year, int monthOfYear,
- int dayOfMonth) {
- Log.d("test", ""+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
- }
- };
-
- /**
- * 复写Activity的onCreateDialog()方法,当调用showDialog()方法的时候,就执行这里
- * 显示DatePickerDialog。并且它的默认date有这里的参数指定,为2011-Fre-20.
- * 月份的1表示二月,0表示一月。
- */
- protected Dialog onCreateDialog(int id) {
- switch(id){
- case DATE_PICKER_ID:
- return new DatePickerDialog(this,onDateSetListener,2011,1,20);
- }
-
- return null;
- }
-
- /**
- * Button的监听器,当按钮按下时就执行showDialog()方法显示
- * DatePickerDialog.并指定ID为DATE_PICKER_ID这个常量。
- */
- class ButtonListener implements OnClickListener{
- public void onClick(View v) {
- showDialog(DATE_PICKER_ID);
- Log.d("test", "display date picker dialog.");
- }
- }
- }