`
ko8e
  • 浏览: 50700 次
  • 性别: Icon_minigender_1
  • 来自: 龙岩
社区版块
存档分类
最新评论

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,有时间把它重新改一改。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics