Easy Tutorial
❮ Html5 Canvas Intro Android Tutorial Expandablelistview ❯

This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation.

English:

2.4.2 Date & Time Components (Part 1)

Category Android Basic Tutorial

Introduction to This Section:

>

This section introduces several time display controls provided by Android, which are: TextClock, AnalogClock, Chronometer, and there is also an outdated DigitalClock that will not be discussed! Let's start the content of this section!


1.TextClock (Text Clock)

>

TextClock is a control introduced after Android 4.2 (API 17) to replace DigitalClock!

You can check whether the system is using a 24-hour time display by calling the is24HourModeEnabled() method provided by TextClock! In 24-hour mode:

In addition, it provides us with the following methods, each corresponding to a get method:

Attribute Name Related Method Description
android:format12Hour setFormat12Hour(CharSequence) Set the format for 12-hour time
android:format24Hour setFormat24Hour(CharSequence) Set the format for 24-hour time
android:timeZone setTimeZone(String) Set the time zone

In fact, more time is spent defining the time format, which is this CharSequence inside! Here are some common usages and results:

<TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="MM/dd/yy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="MMM dd, yyyy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="MMMM dd, yyyy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="E, MMMM dd, yyyy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="EEEE, MMMM dd, yyyy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="Noteworthy day: 'M/d/yy"/>

Running Result:

PS: In addition, the minsdk should be greater than or equal to 17!


2.AnalogClock (Analog Clock)

It's like the picture below:

>

On the official website, we can see these three attributes:

In turn, they are: the background of the watch, the hour hand, and the minute hand of the watch, which we can customize by ourselves:

Sample code is as follows:

<AnalogClock
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:dial="@mipmap/ic_c_bg"
        android:hand_hour="@mipmap/zhen_shi"
        android:hand_minute="@mipmap/zhen_fen" />

Running Result:


3.Chronometer (Stopwatch)

As the title suggests, it is a simple stopwatch, let's go straight to the example of use:

Usage Example:

Implementation Code:

<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=".MainActivity">

    <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="Start Timing" />


```java
chronometer.setBase(SystemClock.elapsedRealtime()); // Reset
                break;
            case R.id.btn_format:
                chronometer.setFormat("Time: %s"); // Change time display format
                break;
        }
    }

    @Override
    public void onChronometerTick(Chronometer chronometer) {
        String time = chronometer.getText().toString();
        if (time.equals("00:00")) {
            Toast.makeText(MainActivity.this, "Time's up~", Toast.LENGTH_SHORT).show();
        }
    }
}

Screenshot of running:


Summary of this section:

This section briefly introduced the TextClock, AnalogClock, and Chronometer components. As you can see from the length, these things are not often used, almost never... Just know about them, and the usage is also very simple... That's it for this section~ Thank you

-10.10 Sensor Topic (1) — Introduction

-10.11 Sensor Topic (2) — Orientation Sensor

-10.12 Sensor Topic (3) — Accelerometer/Gyroscope Sensor

-10.12 Sensor Topic (4) — Understanding Other Sensors

-10.14 Android GPS Introduction

-11.0 "2015 Latest Android Basic Tutorial" Concludes with Celebrations

-12.1 Android Practice: DrySister Girl Viewing App (First Edition) — Project Construction and Simple Implementation

-12.2 DrySister Girl Viewing App (First Edition) — 2. Parsing Backend Data

-12.3 DrySister Girl Viewing App (First Edition) — 3. Image Loading Optimization (Creating a Small Image Caching Framework)

-12.4 DrySister Girl Viewing App (First Edition) — 4. Adding Data Caching (Incorporating SQLite)

-12.5 DrySister Girl Viewing App (First Edition) — 5. Code Review, Adjustment, and Writing of Logging Class

-12.6 DrySister Girl Viewing App (First Edition) — 6. Icon Making, Obfuscation, Signing and Packaging, APK Slimming, App Release

WeChat Follow

❮ Html5 Canvas Intro Android Tutorial Expandablelistview ❯