Android控件之DatePicker、TimePicker

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/dateDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""/>
    
	<DatePicker android:id="@+id/datePicker" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>
	
	<TextView android:id="@+id/timeDisplay"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text=""/>
	<TimePicker android:id="@+id/timerPicker"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center_horizontal"/>
</LinearLayout>

DpTpActivity.java

package com.android.datepicker;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class DpTpActivity extends Activity {
	private TextView date;
	private TextView time;
	private DatePicker mDatePicker;
	private TimePicker mTimePicker;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //findViewById
        mDatePicker = (DatePicker)findViewById(R.id.datePicker);
        mTimePicker = (TimePicker)findViewById(R.id.timerPicker);
        date = (TextView)findViewById(R.id.dateDisplay);
        time = (TextView)findViewById(R.id.timeDisplay);
        
        Calendar mCalendar = Calendar.getInstance();
        final int Year = mCalendar.get(Calendar.YEAR);
        final int Month = mCalendar.get(Calendar.MONTH);
        final int Day = mCalendar.get(Calendar.DAY_OF_MONTH);
        mDatePicker.init(Year, Month, Day,new OnDateChangedListener() {
			
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// TODO Auto-generated method stub
				date.setText(new StringBuilder().append("您选择的时间为:").append(monthOfYear+1).append("-").append(dayOfMonth).append("-").append(year));
			}
		});
        mTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
			
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				time.setText("您选择的时间是:"+hourOfDay+"时"+minute+"分。");
				
			}
		});
        
    }
}


效果图如下
Android控件之DatePicker、TimePicker_第1张图片

你可能感兴趣的:(android,Date,calendar,layout,Class,encoding)