Android DatePicke和rTimePicker一个日期小例子

这是一个日期小例子

import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class DatePicker_TimePicker extends Activity {
	TextView textView;

	// 声明DatePicker对象

	DatePicker datePicker;

	// 声明TimePicker对象

	TimePicker timePicker;

	// 创建Calendar类

	Calendar c;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);

		setContentView(R.layout.date_time);

		// 获得日期实例

		c = Calendar.getInstance();

		// 实例化组件

		Button button1 = (Button) this.findViewById(R.id.button1);

		Button button2 = (Button) this.findViewById(R.id.button2);

		textView = (TextView) this.findViewById(R.id.date_textview);

		datePicker = (DatePicker) this.findViewById(R.id.DatePicker);

		timePicker = (TimePicker) this.findViewById(R.id.TimePicker);

		// DatePicker对象的设置

		// 将日历初始化为当前系统时间,并设置其事件监听

		datePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH),

		c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

			@Override
			public void onDateChanged(DatePicker view, int year,

			int monthOfYear, int dayOfMonth) {

				// 当日期更改时,在这里处理

				// c.set(year, monthOfYear,dayOfMonth);

			}

		});

		// TimePicker对象

		timePicker.setIs24HourView(true);

		// 设置监听事件改变

		timePicker
				.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

					@Override
					public void onTimeChanged(TimePicker view, int hourOfDay,
							int minute) {

						// TODO Auto-generated method stub

						// 时间改变时处理

						// c.set(year, month,day,hourOfDay,minute,second);

						// c.set(hourOfDay, minute);

					}

				});

		// button1事件

		button1.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {

				new DatePickerDialog(DatePicker_TimePicker.this,

				new DatePickerDialog.OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear,

							int dayOfMonth) {

						// TODO Auto-generated method stub

						// c.set(year, monthOfYear, dayOfMonth);

					}

				}, c.get(Calendar.YEAR),

				c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();

			}

		});

		// button2事件

		button2.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {

				new TimePickerDialog(DatePicker_TimePicker.this,

				new TimePickerDialog.OnTimeSetListener() {

					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {

						// c.set(hourOfDay, minute);

					}

				}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true)
						.show();

			}

		});

	}

}

下面是xml文件:

效果如下:

日期时间面板

设置日期

设置时间

暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心