目录
Android TextClock 是一个用于在 Android 应用中显示当前日期和时间的控件。它在 Android 4.2 (API 17) 后推出,并提供了两种不同的格式:24 小时制和 12 小时制。
可以使用 is24HourModeEnabled() 方法来确定系统当前是否在使用 24 小时制。在 24 小时制模式下,系统将按照以下方式进行判断:
下面是 TextClock 控件的主要属性和对应的方法:
属性:
方法:
- "1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:orientation="vertical"
- android:gravity="center">
-
- <TextClock
- android:id="@+id/textClock"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:textSize="24sp"
- android:format24Hour="HH:mm:ss"
- android:format12Hour="hh:mm:ss a"
- android:timeZone="GMT+08:00"/>
- LinearLayout>
Android AnalogClock 控件是一个模拟时钟控件,用于在应用中显示模拟时钟的外观。但是,自 API 23 起,Android 官方已经将 AnalogClock 控件标记为废弃,并建议开发者使用其他替代方案,比如使用自定义绘制的方式或者使用第三方库来实现时钟功能。
在 Android 中,AnalogClock 控件是一个简单的模拟时钟,它显示了当前时间的模拟时钟表盘。尽管从 API 23 开始已经被废弃,但仍然可以使用。下面是 AnalogClock 控件的属性说明:
Android 中的 Chronometer 控件可以用于制作简单的计时器或倒计时功能。它可以显示经过的时间,并且可以方便地开始、停止和重置计时。
默认情况下,Chronometer 控件会以 "MM:SS" 或 "H:MM:SS" 格式显示当前时间,我们也可以使用 setFormat(String) 方法设置为其它的格式
Chronometer 控件可以使用 elapsedRealtime() 方法设置起始时间,如果未设置起始时间,就会在调用 start() 方法时使用当前时间作为起始时间
Chronometer 控件也可以用来开发一个倒计时,可以使用 setCountDown(boolean) 方法来设置剩余时间
- "1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:orientation="vertical"
- android:gravity="center">
-
- <Chronometer
- android:id="@+id/chronometer"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textColor="#ff0000"
- android:textSize="60dip" />
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:orientation="horizontal">
-
- <Button
- android:id="@+id/btnStart"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="开始记时" />
-
- <Button
- android:id="@+id/btnStop"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="停止记时" />
-
- <Button
- android:id="@+id/btnReset"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="重置" />
-
- <Button
- android:id="@+id/btn_format"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="格式化" />
- LinearLayout>
- LinearLayout>
- package com.example.myapplication;
-
- import android.os.Bundle;
- import android.os.SystemClock;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Chronometer;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- public class MainActivity extends AppCompatActivity {
-
- private Chronometer chronometer;
- private Button startButton, stopButton, resetButton, formatButton;
- private boolean isRunning = false;
- private boolean isFormatChanged = false;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- chronometer = findViewById(R.id.chronometer);
- startButton = findViewById(R.id.btnStart);
- stopButton = findViewById(R.id.btnStop);
- resetButton = findViewById(R.id.btnReset);
- formatButton = findViewById(R.id.btn_format);
-
- startButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startChronometer();
- }
- });
-
- stopButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- stopChronometer();
- }
- });
-
- resetButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- resetChronometer();
- }
- });
-
- formatButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- changeChronometerFormat();
- }
- });
- }
-
- private void startChronometer() {
- if (!isRunning) {
- chronometer.setBase(SystemClock.elapsedRealtime());
- chronometer.start();
- isRunning = true;
- }
- }
-
- private void stopChronometer() {
- if (isRunning) {
- chronometer.stop();
- isRunning = false;
- }
- }
-
- private void resetChronometer() {
- chronometer.stop();
- chronometer.setBase(SystemClock.elapsedRealtime());
- isRunning = false;
- }
-
- private void changeChronometerFormat() {
- if (!isFormatChanged) {
- // 第一次点击,更改格式为 "时间:%s"
- chronometer.setFormat("时间:%s");
- isFormatChanged = true;
- } else {
- // 再次点击,恢复到原始格式
- chronometer.setFormat("%s");
- isFormatChanged = false;
- }
- }
- }
- "1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center">
- <Chronometer
- android:id="@+id/chronometer"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:countDown="true"
- android:textColor="#ff0000"
- android:textSize="60dip" />
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:orientation="horizontal">
-
- <Button
- android:id="@+id/btnStart"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="开始" />
-
- <Button
- android:id="@+id/btnReset"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="重置" />
- LinearLayout>
-
- LinearLayout>
- package com.example.myapplication;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Chronometer;
-
- public class MainActivity extends Activity {
-
- private Chronometer chronometer;
- private Button btnStart;
- private Button btnReset;
- private CountDownTimer countDownTimer;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- chronometer = findViewById(R.id.chronometer);
- btnStart = findViewById(R.id.btnStart);
- btnReset = findViewById(R.id.btnReset);
-
- btnStart.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startCountdown();
- }
- });
-
- btnReset.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- resetChronometer();
- }
- });
- }
-
- private void startCountdown() {
- countDownTimer = new CountDownTimer(11000, 1000) {
- @Override
- public void onTick(long millisUntilFinished) {
- long secondsRemaining = millisUntilFinished / 1000;
- chronometer.setText(String.valueOf(secondsRemaining));
- }
-
- @Override
- public void onFinish() {
- chronometer.setText("0");
- showTimeUpDialog();
- }
- }.start();
- }
-
- private void resetChronometer() {
- if (countDownTimer != null) {
- countDownTimer.cancel();
- }
- chronometer.setText("");
- }
-
- private void showTimeUpDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage("时间到了!")
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- // 点击确定按钮后的操作
- }
- });
- builder.create().show();
- }
- }