Android 日期和时间选择控件的开发

一个日期和时间选择控件的例子。来自谷歌开发文档,整合了两个小例子。

import java.util.Calendar;import android.app.Activity;import android.app.DatePickerDialog;import android.app.Dialog;import android.app.TimePickerDialog;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.DatePicker;import android.widget.TextView;import android.widget.TimePicker;public class DatePickerActivity extends Activity {private TextView mDateDisplay;private Button mPickDate;private TextView mTimeDisplay;private Button mPickTime;private int mYear;private int mMonth;private int mDay;private int mHour;private int mMinute;static final int DATE_DIALOG_ID = 0;static final int TIME_DIALOG_ID = 1;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// capture our View elementsmDateDisplay = (TextView) findViewById(R.id.dateDisplay);mPickDate = (Button) findViewById(R.id.pickDate);mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);mPickTime = (Button) findViewById(R.id.pickTime);// add a click listener to the buttonmPickDate.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {showDialog(DATE_DIALOG_ID);}});mPickTime.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {showDialog(TIME_DIALOG_ID);}});// get the current datefinal Calendar c = Calendar.getInstance();mYear = c.get(Calendar.YEAR);mMonth = c.get(Calendar.MONTH);mDay = c.get(Calendar.DAY_OF_MONTH);// get the current timefinal Calendar cc = Calendar.getInstance();mHour = cc.get(Calendar.HOUR_OF_DAY);mMinute = cc.get(Calendar.MINUTE);// display the current date (this method is below)updateDisplay();}// updates the date in the TextViewprivate void updateDisplay() {mDateDisplay.setText(new StringBuilder()// Month is 0 based so add 1.append(mMonth + 1).append("-").append(mDay).append("-").append(mYear).append(" "));mTimeDisplay.setText(new StringBuilder().append(pad(mHour)).append(":").append(pad(mMinute)));}private static String pad(int c) {if (c >= 10)return String.valueOf(c);elsereturn "0" + String.valueOf(c);}// the callback received when the user "sets" the date in the dialogprivate DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {mYear = year;mMonth = monthOfYear;mDay = dayOfMonth;updateDisplay();}};// the callback received when the user "sets" the time in the dialogprivate TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {public void onTimeSet(TimePicker view, int hourOfDay, int minute) {mHour = hourOfDay;mMinute = minute;updateDisplay();}};@Overrideprotected Dialog onCreateDialog(int id) {switch (id) {case DATE_DIALOG_ID:return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,mDay);case TIME_DIALOG_ID:return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,false);}return null;}}

更多相关文章

  1. android之app widget(三)
  2. Android中自定义控件之飞入飞出布局及随机布局实现方式
  3. VideoView控件可以手动改变大小
  4. Android中ExpandableListView控件基本使用
  5. Android动态设置Margin的方法
  6. Android之ListView中的分割线
  7. Android(安卓)TextView内容居中和控件居中
  8. Android(安卓)自动化测试—robotium(五)Spinner控件
  9. 获取Android屏幕尺寸、控件尺寸、状态栏/通知栏高度、导航栏高度

随机推荐

  1. Android面试题集2019版(包含答案整理)
  2. android AppWidget的使用以及利用Service
  3. android与javascript交互调用
  4. Android彻底组件化—UI跳转升级改造
  5. android颜色值的表示方法android:backgro
  6. android设置横屏和竖屏的方法
  7. Android 文件的保存与读取之SDCard(SD卡)存
  8. android中移植 c/c++程序
  9. 解决 Android 在Eclipse 开发中 Class No
  10. android gravity and LinerarLayout for