android教程之日期时间控件DatePicker/TimePicker

       这了两个控件常见在闹钟应用中,通过这两个控件,我们可以很轻松的实现修改日期和时间的功能,在学习这两个控件之前,我们有必要了解一下静态变量,静态方法,

所谓的静态变量,静态方法就是类不需要实例化就可以调用的变量和方法,例子中会涉及到Calendar类的静态方法getInstance(),静态变量YEAR,MONTH等;

      picker.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <DatePicker 
        android:id="@+id/dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    <TimePicker 
        android:id="@+id/tp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/button_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/setdate"/>
    <Button 
        android:id="@+id/button_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/settime"/>
    <TextView 
        android:id="@+id/tvdp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/showdata"/>
    <TextView 
        android:id="@+id/tvtp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/showtime"/>
</LinearLayout>

MainActivity.java

package com.example.demo;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class MainActivity extends Activity {
	private DatePicker datePicker;
	private TimePicker timePicker;
	private Button button_date;
	private Button button_time;
	private TextView tv_date;
	private TextView tv_time;
	Calendar calendar=Calendar.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.picker);
		datePicker=(DatePicker) this.findViewById(R.id.dp);
		timePicker=(TimePicker) this.findViewById(R.id.tp);
		button_date=(Button) this.findViewById(R.id.button_date);
		button_time=(Button) this.findViewById(R.id.button_time);
		tv_date=(TextView) this.findViewById(R.id.tvdp);
		tv_time=(TextView) this.findViewById(R.id.tvtp);
		
		
		datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// TODO Auto-generated method stub
				calendar.set(year, monthOfYear, dayOfMonth);
				tv_date.setText("更改日期为:"+year+"年"+monthOfYear+"月"+dayOfMonth+"日");
			}
		});
		
		timePicker.setIs24HourView(true);
		timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
			
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
				tv_time.setText("更改日期为:"+hourOfDay+"时"+minute+"分");
			}
		});
		
		
		button_date.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
					
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear,
							int dayOfMonth) {
						// TODO Auto-generated method stub
						calendar.set(year, monthOfYear,dayOfMonth);
						tv_date.setText("更改日期为:"+year+"年"+monthOfYear+"月"+dayOfMonth+"日");
					}
				}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
			}
		});
		
		button_time.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
					
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						// //calendar.get(Calendar.HOUR_OF_DAY)代表当前小时,hourOfDay是设置的小时


						calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),hourOfDay,minute);
						tv_time.setText("更改时间"+hourOfDay+"时"+minute+"分");
					}
				}, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();

			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



posted on 2014-01-26 20:11  天神Deity  阅读(286)  评论(0编辑  收藏  举报

导航