1. 在学习之前,我们需要补充一些知识点
2. 掌握上面的知识之后,我们就可以练习使用它的使用方法了,下面我们写一个例子来练习一下1) 查看API文档:android.widget.AnalogClock 和 android.widget.DigitalClock类
这两个是 显示时钟的控件,第一个套件显示了一个带有时针和分针的模拟时钟。第二个是数字时钟2) android.app.TimePickerDialog
这个可以使用在显示时间和日期的控件上面,作为用户的一个时间提示框,我们查看一下这个API文档,里面有一个匿名内部类,是用来监听时间发生改变的时候触发的事件
3) android.app.DatePickerDialog
以及这个类的构造方法,我们也是经常用到的
这个的使用方法和 TimePickerDialog的类似,这里我就不贴出它的方法了,不明白的自己去Android官方文档查一下。
1). 布局文件3. 程序运行结果
<?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" > <AnalogClock android:layout_width="fill_parent" android:layout_height="wrap_content" /> <DigitalClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示TimePickerDialog" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示DatePickerDialog" /> </LinearLayout>
2). Java 代码
public class AnalogClockDemo extends Activity implements OnClickListener { private Button button1, button2; private int hourOfDay, minute; private int year, monthOfYear, dayOfMonth; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); // 获得当前当时间,获得小时和分钟 Calendar calendar = Calendar.getInstance(); hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); minute = calendar.get(Calendar.MINUTE); // 获得当前的秒 year = calendar.get(Calendar.YEAR); monthOfYear = calendar.get(Calendar.MONTH); dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: TimePickerDialog timePickerDialog = new TimePickerDialog(AnalogClockDemo.this, new MyTimePickerDialog(), hourOfDay, minute, true); timePickerDialog.show(); break; case R.id.button2: DatePickerDialog datePickerDialog = new DatePickerDialog(AnalogClockDemo.this, new MyDatePickerDialog(), year, monthOfYear, dayOfMonth); datePickerDialog.show(); break; } } public class MyTimePickerDialog implements TimePickerDialog.OnTimeSetListener { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub Toast.makeText(AnalogClockDemo.this, "hourOfDay" + hourOfDay + "minute" + minute, 1) .show(); } } public class MyDatePickerDialog implements DatePickerDialog.OnDateSetListener { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub Toast.makeText(AnalogClockDemo.this, "year" + year + "monthOfYear" + monthOfYear + "dayOfMonth" + dayOfMonth, 1) .show(); } } }
--->