一、AnalogClock和DigitalClock
AnalogClock和DigitalClock都是Android的时钟组件。不同点在于AnalogClock是一个圆形的表盘时钟,具有时针和分针。DigitalClock则是一个文本样式的时钟,如21:43:15。
这两种时钟都很简单易用,只要在布局文件中配置即可。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <AnalogClock android:id="@+id/analogClock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentTop="true"></AnalogClock> <DigitalClock android:id="@+id/digitalClock" android:text="digitalClock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/analogClock"></DigitalClock> </RelativeLayout>
运行结果:
二、TimePicker和TimePickerDialog
TimePicker用于直接在界面上显示设置时间。TimePickerDialog则是以对话框的形式显示时间设置界面。
TimePicker实例:
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TimePicker android:id="@+id/timePicker" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/timetext" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
TimePickerActivity.java
package com.android.activity; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class TimePickerActivity extends Activity { private TimePicker timePicker = null; private TextView textView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timePicker = (TimePicker)findViewById(R.id.timePicker); textView = (TextView)findViewById(R.id.timetext); timePicker.setOnTimeChangedListener(new TimePickerListener()); } class TimePickerListener implements OnTimeChangedListener{ public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { textView.setText("您设置的时间是: " + hourOfDay + "时" + minute + "分"); } } }
运行结果:
TimePickerDialog实例:
TimePickerDialogActivity.java
package com.android.activity; import java.util.Calendar; import android.app.Activity; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.os.Bundle; import android.widget.TextView; import android.widget.TimePicker; public class TimePickerDialogActivity extends Activity { private TextView textView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView)findViewById(R.id.textview); //得到时间 Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); TimePickerDialogListener timeSet = new TimePickerDialogListener(); //参数分别为:context、监听器、初始时间、初始分钟、是24小时显示false为AM/PM TimePickerDialog timePickerDialog = new TimePickerDialog(this, timeSet, hour, minute, true); timePickerDialog.show(); } class TimePickerDialogListener implements OnTimeSetListener{ //点击设置时即会调用 public void onTimeSet(TimePicker view,int hourOfDay,int minute) { String time = "您设置的时间为:"+hourOfDay+":"+minute; textView.setText(time); } } }
运行结果:设置界面
设置完成后: