简单例子,但是实用,自己再做修改.有时间修改成高级版
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/et1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:hint="输入出发日期"
android:editable="false"
android:textSize="20dp" />
<EditText
android:id="@+id/et2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:hint="输入出发时间"
android:editable="false"
android:textSize="20dp" />
</LinearLayout>
package com.example.blueapp;
import java.util.Calendar;
import android.accounts.OnAccountsUpdateListener;
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.view.Window;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private EditText et1, et2;
private Calendar c;
private int _year, _month, _day, _hour, _minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 无标题
setContentView(R.layout.test1);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
c = Calendar.getInstance();
_year = c.get(Calendar.YEAR);
_month = c.get(Calendar.MONTH);
_day = c.get(Calendar.DAY_OF_MONTH);
_hour = c.get(Calendar.HOUR);
_minute = c.get(Calendar.MINUTE);
et1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(0);
}
});
et2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(1);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 0) {
return new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
_year = year;
_month = monthOfYear;
_day = dayOfMonth;
et1.setText(_year + "-" + (_month + 1) + "-" + _day);
}
}, _year, _month, _day);
} else {
return new TimePickerDialog(this, new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
_hour = hourOfDay;
_minute = minute;
et2.setText(_hour + ":" + _minute);
}
}, _hour, _minute, false);
}
}
public void back(View paramView) {
finish();
}
}