使用DatePickerDialog、TimePickerDialog

    DatePickerDialog与TimerPicker的功能比较简单,用户也简单,只要如下两步即可。

    ①通过new关键字创建DatePickerDialog、TimePickerDialog实例,调用它们的show()方法即可将日期选择对话框、时间选择对话框显示出来。

    ②为DatePickerDialog、TimePickerDialog绑定监听器,这样可以保证用户通过DatePickerDialog、TimePickerDialog设置事件时触发监听器,从而通过监听器来获取用户设置的事件。

    下面的程序中定义了两个按钮,一个按钮用于打开日期选择对话框,一个用于打开时间对话框。该程序的界面布局文件如下:

   

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
     >
     <EditText android:id="@+id/show"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:editable="false"/>
     <LinearLayout 
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:gravity="center_horizontal">
         <Button android:id="@+id/dateBn" 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="设置日期"/>
         <Button android:id="@+id/timeBn" 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="设置时间"/>   
         
     </LinearLayout>
</LinearLayout>

对应的Activity后台代码文件如下:

package org.crazyit.helloworld;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class DateDialogTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.date_dialog_test);
        Button dateBn=(Button)findViewById(R.id.dateBn);
        Button timeBn=(Button)findViewById(R.id.timeBn);
        //为“设置日期”按钮绑定监听器
        dateBn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Calendar c=Calendar.getInstance();
                //直接创建一个DatePickerDialog对话框实例,并将它显示出来
                new DatePickerDialog(DateDialogTest.this,
                        //绑定监听器
                        new DatePickerDialog.OnDateSetListener() {
                            
                            @Override
                            public void onDateSet(DatePicker view, int year, int month,
                                    int dayOfMonth) {
                                // TODO Auto-generated method stub
                                EditText show=(EditText)findViewById(R.id.show);
                                show.setText("您选择啦:"+year+"年"+(month+1)+"月"+dayOfMonth+"日");
                            }
                        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
            }
            
        });
        //为“设置时间”按钮绑定监听器
        timeBn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Calendar c=Calendar.getInstance();
                //创建一个TimePickerDialog实例,并把它显示出来
                new TimePickerDialog(DateDialogTest.this,
                        //绑定监听器
                        new TimePickerDialog.OnTimeSetListener() {
                            
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                // TODO Auto-generated method stub
                                EditText show=(EditText)findViewById(R.id.show);
                                show.setText("您选择了:"+hourOfDay+"时"+minute+"分");
                            }
                        }
                        //设置初始时间
                        ,c.get(Calendar.HOUR_OF_DAY)
                        ,c.get(Calendar.MINUTE)
                        //true表示采用24小时制
                        ,true).show();
            }
            
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.date_dialog_test, menu);
        return true;
    }

}

上面的程序中两段粗体字代码就是创建并显示DatePickerDialog、TimePickerDialog的关键代码。运行上面的程序将会看到如下界面效果。

原文地址:https://www.cnblogs.com/wolipengbo/p/3394577.html