package zhang.Date;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
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 MainActivity extends Activity {
private Button date;
private Button time;
private final int DATE_PICKER_ID=1;
private final int TIME_PICKER_ID=2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
date =(Button)findViewById(R.id.button);
time=(Button)findViewById(R.id.time);
date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
showDialog(DATE_PICKER_ID);
}
});
time.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
showDialog(TIME_PICKER_ID);
}
});
}
//date
DatePickerDialog.OnDateSetListener onDateSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthofyear, int dayofmonth) {
// TODO Auto-generated method stub
System.out.println(year+"-"+monthofyear+"-"+dayofmonth);
}
};
//tiem
OnTimeSetListener onTimeSetListener=new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker v, int h, int m) {
System.out.println(h+"小时"+m+"分");
}
};
protected Dialog onCreateDialog(int id){
switch(id){
case DATE_PICKER_ID:
return new DatePickerDialog(this,onDateSetListener,2011,11,26);
case TIME_PICKER_ID:
return new TimePickerDialog(this,onTimeSetListener,10,10,true);
}
return null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title" android:id="@+id/button"></Button>
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/time" android:id="@+id/time"></Button>
</LinearLayout>