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:
If the time is not obtained, first return the value through getFormat24Hour();
If the acquisition fails, then get the return value through getFormat12Hour();
If both of the above fail, use the default;
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
1.5.1 Git Tutorial on Basic Operations of Local Repositories
1.8 Project Related Analysis (Various Files, Resource Access)
2.4.2 Date & Time Components (Part 1)
[2.4.8 ListView Checkbox
4.4.2 Further Exploration of ContentProvider - Document Provider
5.2.1 In-depth Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 1)
5.2.2 In-depth Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 2)
5.2.3 In-depth Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 3)
5.2.4 In-depth Explanation of Fragment Example - Bottom Navigation Bar + ViewPager Page Switching
6.2 Data Storage and Access - Saving User Preferences with SharedPreferences
6.3.1 Data Storage and Access - An Initial Look at SQLite Database
6.3.2 Data Storage and Access - Another Look at SQLite Database
7.1.1 What to Learn in Android Network Programming and Study of Http Protocol
7.1.2 Learning about Android Http Request and Response Headers
[8.3.3 Paint API - Mask -10.9 WallpaperManager (Wallpaper Manager)
-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.2 DrySister Girl Viewing App (First Edition) — 2. Parsing Backend Data
-12.4 DrySister Girl Viewing App (First Edition) — 4. Adding Data Caching (Incorporating SQLite)