Android控件之-DatePicker和TimePicker

显示日期和时间

这两个类很经常用得到

package com.ko8e;

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 MyActivity extends Activity {
	/** Called when the activity is first created. */
	private TextView view = null;
	private DatePicker date = null;
	private TimePicker time = null;
	private Button button1 = null;
	private Button button2 = null;
	Calendar c;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		view = (TextView) findViewById(R.id.view);
		view.setText(R.string.view);
		// 取得系统时间对象
		c = Calendar.getInstance();
		date = (DatePicker) findViewById(R.id.date);
		time = (TimePicker) findViewById(R.id.time);
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button1.setText(R.string.button1);
		button2.setText(R.string.button2);
		
		date.init(c.get(Calendar.DAY_OF_YEAR), c.get(Calendar.MONTH), c
				.get(Calendar.DAY_OF_MONTH),
				new DatePicker.OnDateChangedListener() {
					public void onDateChanged(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						// 当日期改变时,在这里处理
						c.set(year, monthOfYear, dayOfMonth);
					}
				});
		// 设置为24小时制
		time.setIs24HourView(true);
		time.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				// 当前时间改变时,在这里处理
				c.set(c.get(Calendar.DAY_OF_YEAR), c.get(Calendar.MONTH), c
						.get(Calendar.DAY_OF_MONTH), c
						.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c
						.get(Calendar.SECOND));
			}
		});
		button1.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				new DatePickerDialog(MyActivity.this,
						new DatePickerDialog.OnDateSetListener() {

							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								// 设置日历
							}
						}, c.get(Calendar.DAY_OF_YEAR), c.get(Calendar.MONTH),
						c.get(Calendar.DAY_OF_MONTH)).show();
			}
		});
		button2.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				new TimePickerDialog(MyActivity.this,
						new TimePickerDialog.OnTimeSetListener() {
							public void onTimeSet(TimePicker view,
									int hourOfDay, int minute) {
								// 设置时间
							}
						}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE),
						true).show();
			}
		});
	}
}

main.xml:

<?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:id="@+id/view"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
 <DatePicker
 	android:id="@+id/date"
 	android:layout_width="wrap_content"
 	android:layout_height="wrap_content"
 	/>
 <TimePicker
 	android:id="@+id/time"
 	android:layout_width="wrap_content"
 	android:layout_height="wrap_content"
 	/>
 <Button
 	android:id="@+id/button1"
 	android:layout_width="wrap_content"
 	android:layout_height="wrap_content"
 	/>
<Button
	android:id="@+id/button2"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" 
	/>		
 
</LinearLayout>

具体的代码实现好像有些wrong,有时间把它重新改一改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值