package com.zhycheng;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
public class DatePickerActivity extends Activity implements OnClickListener {
Button button1,button2;
DatePickerDialog dpd;
TimePickerDialog tpd;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2=(Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
dpd=new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
System.out.println(year+";"+monthOfYear//注意:这里的数字是从0到11
+""+dayOfMonth);
}
},2012,8,26);
tpd=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
System.out.println(hourOfDay+";"+minute);
}}, 12, 3, true);//true表示24小时制,false表示12小时制
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.button1)
dpd.show();
if(v.getId()==R.id.button2)
{
tpd.show();
}
}
}
Android 时间和日期对话框
最新推荐文章于 2021-09-10 15:43:24 发布
本文介绍了一个简单的Android应用程序,该程序使用DatePickerDialog和TimePickerDialog来帮助用户选择日期和时间。通过两个按钮分别调用日期和时间选择器对话框,并在选择后打印所选的日期和时间。
摘要由CSDN通过智能技术生成