簡單的DatePicker和TimePicker例子

1.專案資料

Project name: DateAndTimePicker

XML: dateandtimepicker

 

2. 修改dateandtimepicker.xml

a. 更改Layout至LinearLayout1

b. 更改TextView id為@+id/output

c. 加入DatePicker並更改id為@+id/datePicker

d.加入TimePicker並更改id為@+id/timePicker

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;

xmlns:tools=”http://schemas.android.com/tools&#8221;

android:id=”@+id/LinearLayout1″

android:layout_width=”match_parent”

android:layout_height=”match_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=”.DateAndTimePicker” >

 

<TextView

android:id=”@+id/output”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”@string/hello_world” />

 

<DatePicker

android:id=”@+id/datePicker”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content” />

 

<TimePicker

android:id=”@+id/timePicker”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content” />

 

</LinearLayout>

 

 

3. 修改DateAndTimePicker.java

package com.stephensir.dateandtimepicker;

 

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 DateAndTimePicker extends Activity {

 

private int year;

private int month;

private int day;

private int hour;

private int minute;

private TextView output;

private DatePicker datePicker;

private TimePicker timePicker;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.dateandtimepicker);

 

output = (TextView) findViewById(R.id.output);

datePicker = (DatePicker) findViewById(R.id.datePicker);

timePicker = (TimePicker) findViewById(R.id.timePicker);

 

// Use calendar object to get current year, month, day, hour and minute

Calendar calendar = Calendar.getInstance();

year = calendar.get(Calendar.YEAR);

month = calendar.get(Calendar.MONTH);

day = calendar.get(Calendar.DAY_OF_MONTH);

hour = calendar.get(Calendar.HOUR);

minute = calendar.get(Calendar.MINUTE);

 

//initial datePicker value and setOnDateChangedListener to dateChange

datePicker.init(year, month, day, dateChange);

timePicker.setOnTimeChangedListener(timeChange);

 

} // onCreate()

 

private OnDateChangedListener dateChange = new OnDateChangedListener() {

public void onDateChanged(DatePicker view, int year, int monthOfYear,

int dayOfMonth) {

output.setText(Integer.toString(datePicker.getYear()) + “年”

+ Integer.toString(datePicker.getMonth()) + “月”

+ Integer.toString(datePicker.getDayOfMonth()) + “日”

+ Integer.toString(timePicker.getCurrentHour()) + “時”

+ Integer.toString(timePicker.getCurrentMinute()) + “分”

); // output

} // onDateChanged()

}; // dateChange

 

private OnTimeChangedListener timeChange = new OnTimeChangedListener() {

@Override

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

output.setText(Integer.toString(datePicker.getYear()) + “年”

+ Integer.toString(datePicker.getMonth()) + “月”

+ Integer.toString(datePicker.getDayOfMonth()) + “日”

+ Integer.toString(timePicker.getCurrentHour()) + “時”

+ Integer.toString(timePicker.getCurrentMinute()) + “分”

); // output

}

}; // timeChange

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s