标签:
一、AnalogClock和DigitalClock(显示时钟的控件)
二、实例:
在main.xml文件中:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill_parent" 4 android:layout_height="fill_parent"> 5 <AnalogClock android:layout_width="fill_parent" 6 android:layout_height="wrap_content" /> 7 <DigitalClock android:layout_width="wrap_content" 8 android:layout_height="wrap_content" android:textSize="18dp"></DigitalClock> 9 10 <Button android:id="@+id/button1" android:layout_width="fill_parent" 11 android:layout_height="wrap_content" android:text="显示TimePickerDialog"></Button> 12 13 <Button android:id="@+id/button2" android:layout_width="fill_parent" 14 android:layout_height="wrap_content" android:text="显示DatePickerDialog"></Button> 15 </LinearLayout>
在.java文件中:
1 private Button button1, button2; 2 private int hourOfDay, minute; 3 private int year, monthOfYear, dayOfMonth; 4 5 @Override 6 public void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.main); 9 button1 = (Button) this.findViewById(R.id.button1); 10 button2 = (Button) this.findViewById(R.id.button2); 11 button1.setOnClickListener(this); 12 button2.setOnClickListener(this); 13 // 获得当前的时间,获得小时和分钟 14 Calendar calendar = Calendar.getInstance(); 15 hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); 16 minute = calendar.get(Calendar.MINUTE);// 获得当前的秒 17 year = calendar.get(Calendar.YEAR); 18 monthOfYear = calendar.get(Calendar.MONTH); 19 dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); 20 } 21 22 public void onClick(View v) { 23 // TODO Auto-generated method stub 24 switch (v.getId()) { 25 case R.id.button1: 26 // 27 TimePickerDialog timePickerDialog = new TimePickerDialog(Main.this, 28 new MyTimePickerDialog(), hourOfDay, minute, true); 29 timePickerDialog.show();// 显示对话框 30 break; 31 case R.id.button2: 32 DatePickerDialog datePickerDialog = new DatePickerDialog(Main.this, 33 new MyDatePickerDialog(), year, monthOfYear, dayOfMonth); 34 datePickerDialog.show();// 显示对话框 35 break; 36 } 37 } 38 39 public class MyDatePickerDialog implements 40 DatePickerDialog.OnDateSetListener { 41 42 public void onDateSet(DatePicker view, int year, int monthOfYear, 43 int dayOfMonth) { 44 // TODO Auto-generated method stub 45 Toast.makeText( 46 Main.this, 47 "year:" + year + "monthOfYear:" + monthOfYear 48 + "dayOfMonth:" + dayOfMonth, 1).show(); 49 } 50 51 } 52 53 public class MyTimePickerDialog implements 54 TimePickerDialog.OnTimeSetListener { 55 56 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 57 // TODO Auto-generated method stub 58 Toast.makeText(Main.this, 59 "hourOfDay:" + hourOfDay + "minute:" + minute, 1).show(); 60 } 61 62 } 63 }
运行结果:
AnalogClock和DigitalClock时间和日期控件
标签:
原文地址:http://www.cnblogs.com/SoulCode/p/5405927.html