(20120808)(01)android菜单与对话框--之日期及时间选择对话框

清单文件

            
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. "http://schemas.android.com/apk/res/android" 
  3.       package="com.ljq.dialog" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0">  
  6.     "@drawable/icon" android:label="@string/app_name">  
  7.         ".AlertDialog" 
  8.                   android:label="@string/app_name">  
  9.               
  10.                 "android.intent.action.MAIN" />  
  11.                 "android.intent.category.LAUNCHER" />  
  12.               
  13.           
  14.  
  15.       
  16.     "7" />  
  17.     "android.permission.WRITE_CALENDAR" />  
  18.  

main.xml布局文件

 

            
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. "@+id/LinearLayout01" 
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent" 
  4.     android:orientation="vertical" 
  5.     xmlns:android="http://schemas.android.com/apk/res/android">  
  6.     "@+id/et"   
  7.         android:layout_width="fill_parent" 
  8.         android:layout_height="wrap_content"   
  9.         android:editable="false" 
  10.         android:cursorVisible="false" />  
  11.     "日期对话框"   
  12.         android:id="@+id/dateBtn" 
  13.         android:layout_width="fill_parent" 
  14.         android:layout_height="wrap_content" />  
  15.     "时间对话框"   
  16.         android:id="@+id/timeBtn" 
  17.         android:layout_width="fill_parent" 
  18.         android:layout_height="wrap_content" />  
  19.     
  20.         android:text="@+id/digitalClock" 
  21.         android:textSize="20dip"   
  22.         android:gravity="center" 
  23.         android:id="@+id/DigitalClock01"   
  24.         android:layout_width="fill_parent" 
  25.         android:layout_height="wrap_content" />  
  26.     
  27.         android:id="@+id/analogClock" 
  28.         android:gravity="center"   
  29.         android:layout_width="fill_parent" 
  30.         android:layout_height="wrap_content" />  
  31.  

AlertActivity类

 

            
  1. package com.ljq.dialog;  
  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.View;  
  11. import android.widget.Button;  
  12. import android.widget.DatePicker;  
  13. import android.widget.EditText;  
  14. import android.widget.TimePicker;  
  15.  
  16. public class AlertDialog extends Activity {  
  17.     private Button dateBtn = null;  
  18.     private Button timeBtn = null;  
  19.     private EditText et=null;  
  20.     private final static int DATE_DIALOG = 0;  
  21.     private final static int TIME_DIALOG = 1;  
  22.     private Calendar c = null;  
  23.  
  24.     @Override 
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.  
  29.         et=(EditText)findViewById(R.id.et);  
  30.         dateBtn = (Button) findViewById(R.id.dateBtn);  
  31.         timeBtn = (Button) findViewById(R.id.timeBtn);  
  32.         dateBtn.setOnClickListener(new View.OnClickListener(){  
  33.             public void onClick(View v) {  
  34.                 showDialog(DATE_DIALOG);  
  35.             }  
  36.         });  
  37.         timeBtn.setOnClickListener(new View.OnClickListener(){  
  38.             public void onClick(View v) {  
  39.                 showDialog(TIME_DIALOG);  
  40.             }  
  41.         });  
  42.  
  43.     }  
  44.  
  45.     /**  
  46.      * 创建日期及时间选择对话框  
  47.      */ 
  48.     @Override 
  49.     protected Dialog onCreateDialog(int id) {  
  50.         Dialog dialog = null;  
  51.         switch (id) {  
  52.         case DATE_DIALOG:  
  53.             c = Calendar.getInstance();  
  54.             dialog = new DatePickerDialog(  
  55.                 this,  
  56.                 new DatePickerDialog.OnDateSetListener() {  
  57.                     public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {  
  58.                         et.setText("您选择了:" + year + "年" + (month+1) + "月" + dayOfMonth + "日");  
  59.                     }  
  60.                 },   
  61.                 c.get(Calendar.YEAR), // 传入年份  
  62.                 c.get(Calendar.MONTH), // 传入月份  
  63.                 c.get(Calendar.DAY_OF_MONTH) // 传入天数  
  64.             );  
  65.             break;  
  66.         case TIME_DIALOG:  
  67.             c=Calendar.getInstance();  
  68.             dialog=new TimePickerDialog(  
  69.                 this,   
  70.                 new TimePickerDialog.OnTimeSetListener(){  
  71.                     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
  72.                         et.setText("您选择了:"+hourOfDay+"时"+minute+"分");  
  73.                     }  
  74.                 },  
  75.                 c.get(Calendar.HOUR_OF_DAY),  
  76.                 c.get(Calendar.MINUTE),  
  77.                 false 
  78.             );  
  79.             break;  
  80.         }  
  81.         return dialog;  
  82.     }  
  83.  

更多相关文章

  1. Android(安卓)WebView中的JavaScript调用android方法
  2. 修改dialog的大小
  3. Android中实现日期时间选择器(DatePicker和TimePicker)
  4. Android(安卓)-- Dialog(AlertDialog)
  5. Android菜鸟日记14-对话框
  6. Android(安卓)webview注入自己的js代码(js传入function等其他参数
  7. android之计时器 chronometer
  8. Android自定义对话框(Custom Dialog)
  9. Androidの自定义对话框AlertDialog(一)

随机推荐

  1. 自己实现Android关机命令
  2. android ListView之BaseAdapter的使用方
  3. Android之Handler的几个例子程序,对比学习
  4. Android Studio出现Unable to start the
  5. Android.DebugTools.Traceview & dmtrace
  6. android studio AppCompatActivity中onCr
  7. linux命令之ps源码,支持linux和android
  8. Android显示GIF动画完整示例(二)
  9. Android 在程序中重启APP的方法
  10. 【Android】广播的写法