上图:
- 新建一个main.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.datatimepicker.MainActivity" >
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/datePicker"
android:layout_marginRight="16dp"
android:layout_marginTop="158dp" />
</RelativeLayout>
- 主方法的代码如下:
package com.example.datatimepicker;
import java.util.Calendar;
import android.support.v7.app.ActionBarActivity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
/*
* 1.DataPicker对象时以init()这个方法来指定DataPicker初始的年月日以及
* OnDataChangedListener()的事件;而TimePicker对象则是直接以setOnTimeChangedListener()
* 事件来处理时间改变时程序要做的操作。注意MONTH的书写是否要进行加一的操作
* 2.DataPickerDialog与TimePickerDialog与前面这两种类型的对象最大的差别在于
* DataPickerDialog与TimePickerDialog对象则是以弹出Dialog的方式来显示
* */
@SuppressWarnings("unused")
public class MainActivity extends ActionBarActivity {
private TimePicker timePicker ;
private DatePicker datePicker ;
private Calendar cal ;
private int year ;
private int month ;
private int day ;
private int hour ;
private int minute ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取日历的一个对象
cal = Calendar.getInstance() ;
//获取年月日时分秒的信息
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH)+1;
day = cal.get(Calendar.DAY_OF_MONTH);
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
setTitle(year+"-"+month+"-"+day+"-"+hour+":"+minute);
datePicker = (DatePicker) findViewById(R.id.datePicker);
timePicker = (TimePicker) findViewById(R.id.timePicker) ;
//datePicker初始化
datePicker.init(year, cal.get(Calendar.MONTH), day, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
}
});
timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
setTitle(hourOfDay+":"+minute) ;
}
});
// new DatePickerDialog(this, new OnDateSetListener() {
//
// @Override
// public void onDateSet(DatePicker view, int year, int monthOfYear,
// int dayOfMonth) {
// // TODO Auto-generated method stub
// setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
//
// }
// }, year, cal.get(Calendar.MONTH), day).show() ;
new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
setTitle(hourOfDay+":"+minute) ;
}
}, hour, minute, true).show();
}
}
代码中有相关说明,以及注意事项,本文摘自慕课网--高级控件显示。
℃江