界面编程之基本界面组件(6)AnalogClock 和 DigitalClock(时钟)
16lz
2022-07-08
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>
更多相关文章
- 修改android系统和watchdog的延时
- Android简易实战教程--第三十九话《Chronometer实现倒计时》
- Android(安卓)开发中的倒计时
- Android(安卓)Calendar使用过程中遇到的问题
- Android执行shell命令
- Android中的AlarmManager的使用
- android获得系统时间(Handler)
- Android简易闹钟实现
- Flutter 与 Android(安卓)相互调用、传递参数