AnalogClock继承自View,重写了View组件的OnDraw方法,使其在View上显示模拟时钟。


DigitalClock继承自TextView,所以可以使用TextView的XML属性和方法,其本身就是一个文本框,只不过显示的是系统当前时间。


下面是一个很简单的使用示例,只需修改XML布局文件即可。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".Clock" >    <AnalogClock        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <DigitalClock        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="11pt" /></LinearLayout>

另外,Android还提供了一个计时器组件,Chronometer。它也继承自TextView,但它显示的是从某段时间开始过去了多长时间。


Chronometer只提供了一个XML属性:android:format,用于指定计时器的计时格式。


Chronometer支持以下常用方法:setBase(long base):设置计时器的起始时间;


setFormat(String format):设置显示时间的格式;


start():开始计时;


stop():停止计时;


setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener):绑定事件监听器,当计时器改变时触发该监听器。


下面是一个简单的计时器示例。


Java源代码:


package com.example.chronometer;import java.text.ChoiceFormat;import android.os.Bundle;import android.os.SystemClock;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.Chronometer;public class ChronometerTest extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_chronometer_test);final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);Button start = (Button) findViewById(R.id.start);Button stop = (Button) findViewById(R.id.stop);Button reset = (Button) findViewById(R.id.reset);start.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub9chronometer.setBase(SystemClock.elapsedRealtime());chronometer.start();}});stop.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubchronometer.stop();}});reset.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubchronometer.setBase(SystemClock.elapsedRealtime());}});chronometer.setFormat("计时:%s");}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_chronometer_test, menu);return true;}}

XML源代码:


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:stretchColumns="0,1"    tools:context=".ChronometerTest" >    <Chronometer        android:id="@+id/chronometer"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true" />    <TableRow>        <Button            android:id="@+id/start"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Start" />        <Button            android:id="@+id/stop"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5pt"            android:layout_toRightOf="@id/start"            android:text="Stop" />    </TableRow>    <Button        android:id="@+id/reset"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/start"        android:text="Reset" /></TableLayout>


更多相关文章

  1. 修改android系统和watchdog的延时
  2. Android简易实战教程--第三十九话《Chronometer实现倒计时》
  3. Android(安卓)开发中的倒计时
  4. Android(安卓)Calendar使用过程中遇到的问题
  5. Android执行shell命令
  6. Android中的AlarmManager的使用
  7. android获得系统时间(Handler)
  8. Android简易闹钟实现
  9. Flutter 与 Android(安卓)相互调用、传递参数

随机推荐

  1. 用Gson和SharedPreferences轻松构建andro
  2. Android中的PCM设备
  3. Android拍照识别身份证SDK
  4. 在没有内置Google Map add-on的Android真
  5. android 6 中init.rc的生成过程
  6. Android(安卓)PK ios,是谁胜谁负
  7. Android之在GEC210板上点灯。
  8. 基于android和arduino 的小车控制
  9. Android(安卓)Activity间数据传输
  10. android listview 数据加载错乱、错位、