AnalogClock和DigitalClock时间和日期控件

一、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 }

运行结果:

 

转载于:https://www.cnblogs.com/SoulCode/p/5405927.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值