首页 > 移动平台 > 详细

Android---29---DatePicker、TimePick

时间:2015-04-25 21:13:05      阅读:316      评论:0      收藏:0      [点我收藏+]



技术分享




DatePicker与TimePicker是用来显示时间的控件。


onDateChanged、   onTimeChanged分别是控件的触发事件。


MainActivity.java:


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

/**
 * 时间控件的使用
 * 
 * @author Caesar
 * 
 */
public class MainActivity extends Activity implements OnDateChangedListener,
		OnTimeChangedListener {
	private TextView textView;
	private DatePicker datePicker;
	private TimePicker timePicker;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		datePicker = (DatePicker) findViewById(R.id.datepicker);
		textView = (TextView) findViewById(R.id.textview);
		timePicker = (TimePicker) findViewById(R.id.timepicker);
		datePicker.init(2015, 4, 24, this);// 初始化日期
		timePicker.setIs24HourView(true);// 显示时间是否按照24小时制
		timePicker.setOnTimeChangedListener(this);// 注册事件

	}

	// 时间控件的触发
	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
		// TODO Auto-generated method stub
		Toast.makeText(MainActivity.this,
				"hourOfDay" + hourOfDay + "minute" + minute, 1).show();
	}

	// 日期控件的触发
	@Override
	public void onDateChanged(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		// TODO Auto-generated method stub
		Calendar calendar = Calendar.getInstance();
		calendar.set(datePicker.getYear(), datePicker.getMonth(),
				datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
				timePicker.getCurrentMinute());
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日    HH:mm");
		textView.setText(format.format(calendar.getTime()));
	}
}


activity_main.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.datepickerdemo.MainActivity" >

    <DatePicker
        android:id="@+id/datepicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TimePicker
        android:id="@+id/timepicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        />

</LinearLayout>




Android---29---DatePicker、TimePick

原文:http://blog.csdn.net/u013476556/article/details/45273823

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!