引用:http://www.oschina.net/code/snippet_54100_5221
[代码] main.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:Android = "http://schemas.android.com/apk/res/android" |
03 |
Android:orientation = "vertical" |
04 |
Android:layout_width = "fill_parent" |
05 |
Android:layout_height = "fill_parent" |
07 |
< TextView Android:id = "@+id/dateAndTime" |
08 |
Android:layout_width = "fill_parent" |
09 |
Android:layout_height = "wrap_content" |
10 |
Android:text = "@string/hello" |
12 |
< Button Android:id = "@+id/setDate" |
13 |
Android:layout_width = "fill_parent" |
14 |
Android:layout_height = "wrap_content" |
15 |
Android:text = "Set the Date" ></ Button > |
16 |
< Button Android:id = "@+id/setTime" |
17 |
Android:layout_width = "fill_parent" |
18 |
Android:layout_height = "wrap_content" |
19 |
Android:text = "Set the Time" ></ Button > |
[代码] ChronoDemo.java
003 |
import java.text.DateFormat; |
004 |
import java.util.Calendar; |
005 |
import java.util.Locale; |
007 |
import Android.app.Activity; |
008 |
import Android.app.DatePickerDialog; |
009 |
import Android.app.TimePickerDialog; |
010 |
import Android.os.Bundle; |
011 |
import Android.view.View; |
012 |
import Android.widget.Button; |
013 |
import Android.widget.DatePicker; |
014 |
import Android.widget.TextView; |
015 |
import Android.widget.TimePicker; |
017 |
public class ChronoDemo extends Activity { |
019 |
DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance(); |
021 |
TextView dateAndTimeLabel = null ; |
023 |
Calendar dateAndTime = Calendar.getInstance(Locale.CHINA); |
027 |
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() |
030 |
public void onDateSet(DatePicker view, int year, int monthOfYear, |
034 |
dateAndTime.set(Calendar.YEAR, year); |
035 |
dateAndTime.set(Calendar.MONTH, monthOfYear); |
036 |
dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth); |
044 |
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() { |
048 |
public void onTimeSet(TimePicker view, int hourOfDay, int minute) { |
049 |
dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay); |
050 |
dateAndTime.set(Calendar.MINUTE, minute); |
057 |
public void onCreate(Bundle savedInstanceState) { |
058 |
super .onCreate(savedInstanceState); |
059 |
setContentView(R.layout.main); |
062 |
Button dateBtn = (Button)findViewById(R.id.setDate); |
064 |
dateBtn.setOnClickListener( new View.OnClickListener() { |
067 |
public void onClick(View v) { |
069 |
new DatePickerDialog(ChronoDemo. this , |
071 |
dateAndTime.get(Calendar.YEAR), |
072 |
dateAndTime.get(Calendar.MONTH), |
073 |
dateAndTime.get(Calendar.DAY_OF_MONTH)).show(); |
077 |
Button timeBtn = (Button)findViewById(R.id.setTime); |
078 |
timeBtn.setOnClickListener( new View.OnClickListener() { |
082 |
public void onClick(View v) { |
083 |
new TimePickerDialog(ChronoDemo. this , |
085 |
dateAndTime.get(Calendar.HOUR_OF_DAY), |
086 |
dateAndTime.get(Calendar.MINUTE), |
092 |
dateAndTimeLabel=(TextView)findViewById(R.id.dateAndTime); |
098 |
private void updateLabel() { |
099 |
dateAndTimeLabel.setText(fmtDateAndTime |
100 |
.format(dateAndTime.getTime())); |