9.Android之日期对话框DatePicker控件学习

设置日期对话框在手机经常用到,今天来学习下。

首先设置好布局文件:如图

xml对应代码

 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout   
 3     android:id="@+id/LinearLayout01"      
 4     android:layout_width="fill_parent"   
 5     android:layout_height="fill_parent"      
 6     android:orientation="vertical"      
 7     xmlns:android="http://schemas.android.com/apk/res/android"> 
 8     
 9     <EditText
10         android:id="@+id/edit1"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:editable="false"/>
14     
15     <Button
16         android:id="@+id/btnDate"
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:text="日期对话框"/>
20     <Button
21         android:id="@+id/btnTime"
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content"
24         android:text="时间对话框"/>
25 
26     <DigitalClock
27         android:id="@+id/digitalClock1"
28         android:gravity="center"
29         android:layout_width="match_parent"
30         android:layout_height="wrap_content"
31         android:text="DigitalClock" />
32 
33     <AnalogClock
34         android:id="@+id/analogClock1"
35         android:gravity="center"
36         android:layout_width="match_parent"
37         android:layout_height="wrap_content" />
38     
39 </LinearLayout>

注意:EditText里面android:editable="false"这句作用是让编辑框处于不可编辑状态

接下来我们修改下MainActivity.java代码,如下:

  1 package com.example.datepickerdialog;
  2 
  3 import java.util.Calendar;
  4 
  5 import android.app.Activity;
  6 import android.app.DatePickerDialog;
  7 import android.app.Dialog;
  8 import android.app.TimePickerDialog;
  9 import android.os.Bundle;
 10 import android.view.Menu;
 11 import android.view.MenuItem;
 12 import android.view.View;
 13 import android.widget.Button;
 14 import android.widget.DatePicker;
 15 import android.widget.EditText;
 16 import android.widget.TimePicker;
 17 
 18 public class MainActivity extends Activity {
 19 
 20     private EditText m_edit = null;
 21     private Button m_btnDate = null;
 22     private Button m_btnTime = null;
 23     private final static int DIALOG_DATE = 0;
 24     private final static int DIALOG_TIME = 1;
 25     private Calendar c = null;
 26     
 27     @Override
 28     protected void onCreate(Bundle savedInstanceState) {
 29         super.onCreate(savedInstanceState);
 30         setContentView(R.layout.activity_main);
 31         
 32         m_edit = (EditText)findViewById(R.id.edit1);
 33         m_btnDate = (Button)findViewById(R.id.btnDate);
 34         m_btnTime = (Button)findViewById(R.id.btnTime);
 35              
 36         m_btnDate.setOnClickListener(new View.OnClickListener() {
 37             
 38             @Override
 39             public void onClick(View v) {
 40                  showDialog(DIALOG_DATE);        
 41             }
 42         });
 43         
 44         m_btnTime.setOnClickListener(new View.OnClickListener() {
 45             
 46             @Override
 47             public void onClick(View v) {
 48                 showDialog(DIALOG_TIME);         
 49             }
 50         });
 51     }
 52     
 53     protected Dialog onCreateDialog(int id){
 54         
 55         Dialog dialog = null;
 56         switch(id){
 57         
 58         case DIALOG_DATE:
 59             c = Calendar.getInstance(); 
 60              dialog = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {  
 61                  public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {  
 62                      m_edit.setText("您选择了:" + year + "年" + (month+1) + "月" + dayOfMonth + "日");   
 63                  }   
 64              }, c.get(Calendar.YEAR), // 传入年份                
 65              c.get(Calendar.MONTH), // 传入月份                 
 66              c.get(Calendar.DAY_OF_MONTH) // 传入天数 
 67              );
 68             break;
 69             
 70         case DIALOG_TIME:
 71             c=Calendar.getInstance();              
 72             dialog=new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener(){   
 73                 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {    
 74                     m_edit.setText("您选择了:"+hourOfDay+"时"+minute+"分");                   
 75                 }  
 76             },  
 77             c.get(Calendar.HOUR_OF_DAY),  //小时
 78             c.get(Calendar.MINUTE),       //分钟
 79             false  
 80             );  
 81             break;
 82         }
 83         return dialog;        
 84     }
 85     
 86 
 87     @Override
 88     public boolean onCreateOptionsMenu(Menu menu) {
 89         // Inflate the menu; this adds items to the action bar if it is present.
 90         getMenuInflater().inflate(R.menu.main, menu);
 91         return true;
 92     }
 93 
 94     @Override
 95     public boolean onOptionsItemSelected(MenuItem item) {
 96         // Handle action bar item clicks here. The action bar will
 97         // automatically handle clicks on the Home/Up button, so long
 98         // as you specify a parent activity in AndroidManifest.xml.
 99         int id = item.getItemId();
100         if (id == R.id.action_settings) {
101             return true;
102         }
103         return super.onOptionsItemSelected(item);
104     }
105 }

提示:在这里我犯了一个小错误,就是重写onCreateDialog函数时我错写成OnCreateDialog,结果运行程序,点击日期和时间对话框都没有效果显示,所以大家在写代码时要注意这些细节。

最后运行效果:

       

      

原文地址:https://www.cnblogs.com/benchao/p/5073810.html