1. xml中的代码:
<LinearLayout 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:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:id="@+id/linear_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_time"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/icon_time"/>
<Button
android:id="@+id/btn_time_picker"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#f3f3f3"
android:layout_marginLeft="5dp"
android:text="时间"
android:textColor="#ffffff"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_date"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/icon_date"/>
<Button
android:id="@+id/btn_date_picker"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#f3f3f3"
android:layout_marginLeft="5dp"
android:text="日期"
android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
2. java中的代码:
package com.zq.test;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
public class DatePickerAndTimePickerActivity extends Activity {
private Button btnTimePicker;
private Button btnDatePicker;
private int time_hour, time_minute;
private int date_year, date_month, date_day;
private OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time_hour = hourOfDay;
time_minute = minute;
updateTimeDisplay();
}
};
private OnDateSetListener onDateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
date_year = year;
date_month = monthOfYear;
date_day = dayOfMonth;
updateDateDisplay();
}
};
static final int TIME_DIALOG_ID = 0;
static final int DATE_DIALOG_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker_and_time_picker);
btnTimePicker = (Button)findViewById(R.id.btn_time_picker);
btnTimePicker.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
btnDatePicker = (Button)findViewById(R.id.btn_date_picker);
btnDatePicker.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar calendar = Calendar.getInstance();
time_hour = calendar.get(Calendar.HOUR_OF_DAY);
time_minute = calendar.get(Calendar.MINUTE);
date_year = calendar.get(Calendar.YEAR);
date_month = calendar.get(Calendar.MONTH);
date_day = calendar.get(Calendar.DAY_OF_MONTH);
updateTimeDisplay();
updateDateDisplay();
}
private void updateTimeDisplay() {
btnTimePicker.setText(new StringBuilder().
append(pad(time_hour)).
append(":").
append(pad(time_minute)));
}
private void updateDateDisplay() {
btnDatePicker.setText(new StringBuilder().
append(date_year).
append("-").
append(date_month + 1).
append("-").
append(date_day));
}
private static String pad(int index) {
if(index >= 10)
return String.valueOf(index);
else
return "0" + String.valueOf(index);
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case TIME_DIALOG_ID :
return new TimePickerDialog(this, onTimeSetListener, time_hour, time_minute, true);
case DATE_DIALOG_ID :
return new DatePickerDialog(this, onDateSetListener, date_year, date_month, date_day);
}
return null;