正文  UI设计 > 选择器(Picker) >

android学习之--创建DatePickerDialog的步骤

1.声明一个监听器,使用匿名内部类:  2.复写onCreateDialog(int id):  3.当你需要的时候调用showDialog方法: 代码如下:  pack...

1.声明一个监听器,使用匿名内部类:
 
 
2.复写onCreateDialog(int id):
 
 
3.当你需要的时候调用showDialog方法:
 
代码如下:
 
 
package com.example.datepickerdialog1;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.app.DatePickerDialog;  
import android.app.Dialog;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.DatePicker;  
  
public class MainActivity extends Activity  
{  
    private static final int DATE_PICKER_ID=1;//静态常量  
    private Button showButton;  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        showButton=(Button) findViewById(R.id.myButton);  
        showButton.setText("显示日历");  
        showButton.setOnClickListener(new OnClickListener()  
        {  
              
            @Override  
            public void onClick(View arg0)  
            {  
                //写1就行  
                showDialog(DATE_PICKER_ID);  
                  
            }  
        });  
    }  
    //监听器,用户点下set后设置日期  
    DatePickerDialog.OnDateSetListener onDateSetListener=new DatePickerDialog.OnDateSetListener()  
    {  
          
        @Override  
        public void onDateSet(DatePicker view, int year, int monthOfYear,  
                int dayOfMonth)  
        {  
            System.out.println(year+","+monthOfYear+","+dayOfMonth);  
              
        }  
    };  
    @Override  
    protected Dialog onCreateDialog(int id)  
    {  
        switch(id)  
        {  
        case DATE_PICKER_ID:  
        return new DatePickerDialog(this, onDateSetListener, 2013, 10, 18);  
        }  
        return null;  
          
    }  
  
}