Android入門之TextClock的使用教程
介紹
TextClock是在Android 4.2(API 17)后推出的用來(lái)替代DigitalClock的一個(gè)控件。
TextClock可以以字符串格式顯示當(dāng)前的日期和時(shí)間,因此推薦在Android 4.2以后使用TextClock。
這個(gè)控件推薦在24進(jìn)制的android系統(tǒng)中使用,TextClock提供了兩種不同的格式, 一種是在24進(jìn)制中顯示時(shí)間和日期,另一種是在12進(jìn)制中顯示時(shí)間和日期。大部分人喜歡默認(rèn)的設(shè)置。
可以通過(guò)調(diào)用:TextClock提供的is24HourModeEnabled()方法來(lái)查看,系統(tǒng)是否在使用24進(jìn)制時(shí)間顯示! 在24進(jìn)制模式中:
- 如果沒(méi)獲取時(shí)間,首先通過(guò)getFormat24Hour()返回值;
- 獲取失敗則通過(guò)getFormat12Hour()獲取返回值;
- 以上都獲取失敗則使用默認(rèn);

它的使用非常簡(jiǎn)單。
課程例子
我們通過(guò)5種格式來(lái)說(shuō)明一下這個(gè)TextClock的使用。
在例子中,我們做了一個(gè)按鈕,這個(gè)按鈕會(huì)對(duì)第3行的TextClock根據(jù)系統(tǒng)是否Enable24 Hour來(lái)把它的顯示改成:24小時(shí)的顯示格式。
UI主界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<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:id="@+id/timeTextClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="MMMM dd, yyyy h:mm:aa"/>
<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"/>
<Button
android:id="@+id/setTimeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>代碼
package org.mk.android.demo.demotxtclock;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextClock;
public class MainActivity extends AppCompatActivity {
private TextClock timeClock;
private Button setTimeButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTimeButton = (Button) findViewById(R.id.setTimeButton);
timeClock = (TextClock) findViewById(R.id.timeTextClock);
setTimeButton.setOnClickListener(new OnClickListener());
}
private class OnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
String clockFormatter = "MMMM dd, yyyy h:mm:ss";
if (timeClock.is24HourModeEnabled()) {
Log.i("app", ">>>>>>System has been enabled the 24Hour Model");
timeClock.setFormat24Hour(clockFormatter);
} else {
Log.i("app", ">>>>>>System has not been enabled the 24Hour " +
"Model");
timeClock.setFormat12Hour(clockFormatter);
}
}
}
}運(yùn)行效果
當(dāng)我們點(diǎn)下了按鈕后,可以看到界面上第三行的TextClock顯示的值發(fā)生了變化,如下截圖。
自己動(dòng)一下手試試看效果吧。

到此這篇關(guān)于Android入門之TextClock的使用教程的文章就介紹到這了,更多相關(guān)Android TextClock內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android服務(wù)應(yīng)用ClockService實(shí)現(xiàn)鬧鐘功能
- Android自定義DigitalClock控件實(shí)現(xiàn)商品倒計(jì)時(shí)
- Android ScreenLockReceiver監(jiān)聽(tīng)鎖屏功能示例
- Android應(yīng)用程序保持后臺(tái)喚醒(使用WakeLock實(shí)現(xiàn))
- Android DigitalClock組件用法實(shí)例
- Android AnalogClock簡(jiǎn)單使用方法實(shí)例
- Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法實(shí)例詳解
- Android控件之AnalogClock與DigitalClock用法實(shí)例分析
- Android Lock鎖實(shí)現(xiàn)原理詳細(xì)分析
相關(guān)文章
Android使用webView長(zhǎng)按保存下載網(wǎng)絡(luò)圖片
這篇文章主要為大家詳細(xì)介紹了Android使用webView長(zhǎng)按保存下載網(wǎng)絡(luò)圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Android使用PullToRefresh完成ListView下拉刷新和左滑刪除功能
ListView下刷新刷功能相信從事Android開(kāi)發(fā)的猿友們并不陌生,本文就帶領(lǐng)一些剛?cè)腴Tandroid的朋友或者一起愛(ài)分享的朋友來(lái)簡(jiǎn)單的實(shí)現(xiàn)ListView的下拉刷新和左滑刪除效果。感興趣的朋友一起看看吧2016-11-11
android使用service和activity獲取屏幕尺寸的方法
這篇文章主要介紹了android使用service和activity獲取屏幕尺寸的方法,實(shí)例分析了基于service和activity兩種方法獲取屏幕尺寸的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-08-08
Android自定義組合控件之自定義下拉刷新和左滑刪除實(shí)例代碼
最近做了個(gè)項(xiàng)目,其中有項(xiàng)目需求,用到下拉刷新和左滑刪除,網(wǎng)上沒(méi)有找到比較理想的解決辦法。下面小編給大家分享我的一個(gè)小demo有關(guān)Android自定義組合控件之自定義下拉刷新和左滑刪除實(shí)例代碼,需要的朋友參考下2016-04-04
Android自定義控件開(kāi)發(fā)實(shí)戰(zhàn)之實(shí)現(xiàn)ListView下拉刷新實(shí)例代碼
這篇文章主要介紹了Android自定義控件開(kāi)發(fā)實(shí)戰(zhàn)之實(shí)現(xiàn)ListView下拉刷新實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)自動(dòng)填充短信驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)填充短信驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android EasyPermissions官方庫(kù)高效處理權(quán)限相關(guān)教程
Easypermissions簡(jiǎn)化了Android M的運(yùn)行時(shí)權(quán)限的申請(qǐng)、結(jié)果處理、判斷等步驟。這篇文章主要介紹了Android EasyPermissions官方庫(kù)高效處理權(quán)限相關(guān)教程,需要的朋友可以參考下2017-11-11

