android 开发DatePickerDialog/TimePickerDialog对话框的实现
AndroidAPI提供了Dialog对话框控件,DatePickerDialog/TimePickerDialog均是AlertDialog的子类,通过DatePickerDialog/TimePickerDialog日期/时间的对话框来进行时间的选择。
layout页面:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <Button 12 android:id="@+id/button2" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_alignLeft="@+id/button1" 16 android:layout_alignParentBottom="true" 17 android:layout_alignRight="@+id/button1" 18 android:layout_marginBottom="29dp" 19 android:text="时间对话框" /> 20 21 <Button 22 android:id="@+id/button1" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_above="@+id/button2" 26 android:layout_alignParentLeft="true" 27 android:layout_marginBottom="25dp" 28 android:layout_marginLeft="72dp" 29 android:text="日期对话框" /> 30 31 <EditText 32 android:id="@+id/editText1" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:layout_alignParentTop="true" 36 android:layout_centerHorizontal="true" 37 android:layout_marginTop="22dp" 38 android:ems="10"/> 39 40 </RelativeLayout>
activity:
1 package com.example.android_date_timedialog; 2 3 import java.util.Calendar; 4 5 import android.app.Activity; 6 import android.app.DatePickerDialog; 7 import android.app.DatePickerDialog.OnDateSetListener; 8 import android.app.TimePickerDialog; 9 import android.app.TimePickerDialog.OnTimeSetListener; 10 import android.os.Bundle; 11 import android.view.Menu; 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 import android.widget.Toast; 18 /** 19 * @author xiaowu 20 * NOTE:日期时间对话框控件,继承自AlertDialog对话框 21 */ 22 public class MainActivity extends Activity { 23 private Button button ; 24 private Button button2 ; 25 private EditText editText ; 26 //日期对话框 27 private DatePickerDialog datePickerDialog ; 28 //时间选择对话框 29 private TimePickerDialog timePickerDialog; 30 private int year, monthOfYear, dayOfMonth, hourOfDay, minute, is24HourView ; 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 button = (Button) findViewById(R.id.button1); 36 button2 = (Button) findViewById(R.id.button2); 37 editText = (EditText) findViewById(R.id.editText1); 38 //初始化日历 39 Calendar calendar = Calendar.getInstance(); 40 year = calendar.get(Calendar.YEAR); 41 monthOfYear = calendar.get(Calendar.MONTH); 42 dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); 43 hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); 44 minute = calendar.get(Calendar.MINUTE); 45 //初始化日期datePickerDialog, 注册日期选择监听器 46 datePickerDialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() { 47 @Override 48 public void onDateSet(DatePicker view, int year, int monthOfYear, 49 int dayOfMonth) { 50 // TODO Auto-generated method stub 51 String text = year+"-"+(monthOfYear+1)+"-"+dayOfMonth ; 52 editText.setText(text); 53 } 54 }, year, monthOfYear, dayOfMonth); 55 //按钮点击后显示日期选择datePickerDialog 56 button.setOnClickListener(new View.OnClickListener() { 57 @Override 58 public void onClick(View v) { 59 // TODO Auto-generated method stub 60 datePickerDialog.show(); 61 } 62 }); 63 //初始化时间timePickerDialog, 注册时间选择监听器 64 timePickerDialog = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() { 65 @Override 66 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 67 // TODO Auto-generated method stub 68 Toast.makeText(MainActivity.this, hourOfDay+":"+minute, 1).show(); 69 } 70 }, hourOfDay, minute, true); 71 //按钮点击后显示时间选择timePickerDialog 72 button2.setOnClickListener(new View.OnClickListener() { 73 @Override 74 public void onClick(View v) { 75 // TODO Auto-generated method stub 76 timePickerDialog.show(); 77 } 78 }); 79 } 80 81 @Override 82 public boolean onCreateOptionsMenu(Menu menu) { 83 // Inflate the menu; this adds items to the action bar if it is present. 84 getMenuInflater().inflate(R.menu.main, menu); 85 return true; 86 } 87 88 }